为动态查询的where子句生成谓词(''String literal')

时间:2018-06-18 14:28:46

标签: c# sql sql-server

我有两个问题。

第一次查询: id = ("select PreferredTimeSlot1 from RoutineInfo where TeacherInitials = 'NM'");

第二次查询: string query = ("select ordinal_position from information_schema.columns c where table_name = 'FinalRoutine' and table_schema = 'dbo' and column_name ='" + id + "'");

我想使用id变量从第二个查询中动态提取ordinal_position。

理论上这些查询应该有效。但是我得到了syntax error near =

感谢任何帮助。这里的示例代码相当大。但如果需要,我也可以提供。

这是代码。我希望这段代码能够更多地澄清我的问题。

public void ColumnIDPulling()
    {
       int Column;
       try
        {

            string id,selection="NM";
            conn = new SqlConnection(@"Data Source=MIRAZ-PC\SQLEXPRESS;Initial Catalog=ClassRoutine_1;Integrated Security=True");
            conn.Open();
            id = "select PreferredTimeSlot1 from RoutineInfo where TeacherInitials = 'NM'";
            reader = new SqlCommand(id, conn).ExecuteReader();
            textBox1.Text = Convert.ToString(id);
            //id = textBox2.Text;
            reader.Close();
            string query = ("select ordinal_position from information_schema.columns c where table_name = 'FinalRoutine' and table_schema = 'dbo' and column_name ='" + id + "'");
            //string query = ("select ordinal_position from information_schema.columns c where table_name = 'FinalRoutine' and table_schema = 'dbo' and column_name =[9:15-10:30]");
            reader = new SqlCommand(query, conn).ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Column = reader.GetInt32(0);
                    textBox1.Text = Convert.ToString(Column);
                   // textBox1.Text = Convert.ToString(id);

                }
            }
            else
            {
                textBox1.Text = "NF";
            }
            reader.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
       conn.Close();
    }

1 个答案:

答案 0 :(得分:2)

当你想从数据库中获取单个值时,你需要使用ExecuteScalar,你只需返回" PreferredTimeSlot1"。当您需要返回完整的行时,我们使用ExecuteReader。您遇到的问题是您使用ExecuteReader,然后尝试将行转换为字符串而不是值

public void ColumnIDPulling()
    {
        int Column;
        try
        {

            string query, id, selection = "NM";
            conn = new SqlConnection(@"Data Source=MIRAZ-PC\SQLEXPRESS;Initial Catalog=ClassRoutine_1;Integrated Security=True");
            conn.Open();
            query = "select PreferredTimeSlot1 from RoutineInfo where TeacherInitials = 'NM'";
            id = new SqlCommand(query, conn).ExecuteScalar().ToString();
            if (id != null)
            {
                textBox1.Text = id;

                query = string.Format("select ordinal_position from information_schema.columns c where table_name = 'FinalRoutine' and table_schema = 'dbo' and column_name ='{0}'",id);
                //string query = ("select ordinal_position from information_schema.columns c where table_name = 'FinalRoutine' and table_schema = 'dbo' and column_name =[9:15-10:30]");
                reader = new SqlCommand(query, conn).ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Column = reader.GetInt32(0);
                        textBox1.Text = Convert.ToString(Column);
                        // textBox1.Text = Convert.ToString(id);

                    }
                }
                else
                {
                    textBox1.Text = "NF";
                }
            }

            reader.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        if (conn != null) conn.Close();
    }