c# - C# 中的多个 SQL 查询将变量作为列返回

标签 c# sql sql-server

我正在开展一个创建注册系统的学校项目。我选择的数据库方法是使用 T-SQL,因为它是我已经熟悉的东西。

我使用下面的代码来查询数据库

    public void button3_Click(object sender, EventArgs e)
    {
        string StudentID = (textBox1.Text);
        string queryDOB = "SELECT DOB,LastName,Degree FROM Student Where StudentID = " + StudentID;

        SqlConnection con = new SqlConnection(Properties.Settings.Default.SRSConnectionString);
        con.Open();

        SqlCommand DOB = new SqlCommand(queryDOB, con);

        SqlDataReader stidreader = DOB.ExecuteReader();

        if (stidreader.Read())
        {
            textBox2.Text = stidreader["DOB"].ToString();
            textBox3.Text = stidreader["LastName"].ToString();
            textBox5.Text = stidreader["Degree"].ToString();
        }
        else
        {
            MessageBox.Show("Your ID is not recognised. Please try again.");
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox5.Clear();
        }

        con.Close();
    }

    private void textBox5_TextChanged(object sender, EventArgs e)
    {
        string Degree = textBox5.Text;
        string degsql = "SELECT ModName FROM Module Where Degree = " + Degree;
        SqlConnection con = new SqlConnection(Properties.Settings.Default.SRSConnectionString);
        DataSet dsm = new DataSet();
        SqlDataAdapter connect = new SqlDataAdapter(degsql, con);

        con.Open();

        connect.Fill(dsm);
        this.listBox2.DataSource = dsm.Tables[0];
        this.listBox2.DisplayMember = "ModName";
}

按钮单击的第一部分工作正常,但是当我在下一个事件(文本框更改)中运行查询时,查询返回一条错误消息,指出发现了无效列。好吧,找到的列是应该用于查询的变量,而不是列本身,这非常令人困惑。

我知道 MARS 可能存在问题,但 DB 似乎是正确的版本。

有人能透露一下吗?

欣赏,

杰米

最佳答案

您的变量名称缺少单引号。将您的代码更改为以下内容以修复 SQL:

string degsql = "SELECT ModName FROM Module Where Degree = '" + Degree + "'";

或者这看起来稍微好一点:

string degsql = string.Format(
    "SELECT ModName FROM Module Where Degree = '{0}'", 
    Degree);

但是,您不应该这样做,因为它会让您遭受 SQL 注入(inject)攻击。阅读这个问题以了解这意味着什么:How do parameterized queries help against SQL injection?

这里有一些代码可以正确执行此操作并避免那些令人讨厌的安全问题:

private void textBox5_TextChanged(object sender, EventArgs e)
{
    string Degree = textBox5.Text;
    string degsql = "SELECT ModName FROM Module Where Degree = @Degree";

    DataSet dsm = new DataSet();

    using(var con = new SqlConnection(Properties.Settings.Default.SRSConnectionString))
    {
        SqlCommand cmd = new SqlCommand(degsql, con);
        // use the appropriate SqlDbType:
        var parameter = new SqlParameter("@Degree", SqlDbType.Varchar);
        parameter.Value = Degree;
        cmd.Parameters.Add(parameter);

        SqlDataAdapter connect = new SqlDataAdapter(cmd);
        connect.Fill(dsm);
    }

    this.listBox2.DataSource = dsm.Tables[0];
    this.listBox2.DisplayMember = "ModName";

关于c# - C# 中的多个 SQL 查询将变量作为列返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27246996/

相关文章:

mysql - 未找到函数 "TO_DATE"; SQL语句: in H2 database and not using TO_CHAR

mysql - 是否可以阻止与表中 2 个不同列相关的 sql 插入/更新?

sql - 使用评分在 SQL 中查找最佳匹配

c# - 在mvc布局页面显示动态菜单

c# - 多个场景的if语句

c# - Visual Studio 不断删除 form.designers 中的代码

c# - 限制对某些 .aspx 页面的访问

c# - string.GetHashCode 和 IEqualityComparer<string>.Default.GetHashCode 之间的区别

sql - 错误的 SQL 统计信息如何导致对 40 行表的请求花费超过一分钟?

SQL Server 查询 - 如果不匹配则返回空值