c# - 需要在 C# 中使用字符串从数据库中查找 ID 号

标签 c# asp.net sql-server

我需要从标签中获取数据,这些数据是我使用该标签的 session 从上一页返回的,我需要使用它来查找该数据的 ID,例如,如果标签包含单词“IT”,则需要在其中找到其 ID数据库D_ID=5代码如下

public partial class FinalFeedback1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GetDataFromSession();
        GetDID();
        AddDynamicLabels();
    }

public void GetDID()
{
    var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        SqlDataReader myReader1 = null;
        string depart = "select D_ID from Department where D_Name= " + Label8.Text + "";
        SqlCommand cmd1 = new SqlCommand(depart, connection);
        myReader1 = cmd1.ExecuteReader(); // i am getting error here "Invalid column name 'IT'"
        while (myReader1.Read()) 
        {
            Label9.Text = myReader1["D_ID"].ToString(); 
        }
    }
}
public void AddDynamicLabels()
{
    var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        SqlDataReader myReader2 = null;
        string CmdString = "Select Q_ID,Question_Data FROM QuestionTable where D_ID=" + Label9.Text + "";
        SqlCommand cmd = new SqlCommand(CmdString, connection);
        myReader2 = cmd.ExecuteReader();
        while (myReader2.Read())
            {
                QID1.Text = myReader2["Q_ID"].ToString();
                if (QID1.Text == ("1"))
                {
                    Question1.Text = myReader2["Question_Data"].ToString();
                }
                else if (QID1.Text ==("2"))
                {
                    Question2.Text = myReader2["Question_Data"].ToString();
                }
                else if (QID1.Text == ("3"))
                {
                    Question3.Text = myReader2["Question_Data"].ToString();
                }
                else if (QID1.Text == ("4"))
                {
                    Question4.Text = myReader2["Question_Data"].ToString();
                }
                else if (QID1.Text == ("5"))
                {
                    Question5.Text = myReader2["Question_Data"].ToString();
                }
            }
     }
}

private void GetDataFromSession()
{
    Label2.Text = Session["SNL"].ToString();
    Label4.Text = Session["SNB"].ToString();
    Label6.Text = Session["EMPID"].ToString();
    Label8.Text = Session["DNAME"].ToString();
}

最佳答案

更改此行。

string depart = "select D_ID from Department where D_Name= "+ Label8.Text + "";

到这一行

string depart = "select D_ID from Department where D_Name= '"+ Label8.Text + "'";

查看第二行中的单引号。您的 string 值未用单引号引起来,这就是原因。

编辑:您的代码对 SQL Injection Attack 开放.您应该使用 SqlParameter 而不是连接查询。

如需更多阅读,您可以使用此链接: http://www.w3schools.com/sql/sql_injection.asp

关于c# - 需要在 C# 中使用字符串从数据库中查找 ID 号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38928934/

相关文章:

c# - 如何使 DropDownList 控件以粗体显示某些项目 ASP.NET

C# - 从串行端口缓冲区读取

javascript - 具有多个 Web 表单的 jQuery 选项卡

c# - 我如何获得更多 SQL Server 和 T-SQL 经验

c# - 当前文件夹中的 .dll 出现 DllNotFoundException

c# - 使用 AutoFixture 为递归数据结构创建夹具

c# - 多语言asp.net类项目

javascript - 如何将数据绑定(bind)值分配给 JavaScript 变量

sql-server - 是否可以在填充后将 SQL Server 表列数据类型从 bigint 更改为 varchar?

mysql - 如何将 mysql 触发器转换/迁移到 Sql Server 触发器