c#查询组合框所选文本

时间:2016-01-18 19:45:06

标签: c#

我正在创建一个自动生成时间表的Windows窗体应用程序。当用户从下拉列表中选择学期编号(从1-8开始)然后我应该在查询中写什么时,我在编写查询时遇到问题?我的代码如下。

private void button_generate_Click(object sender, EventArgs e)
{
    SqlCommand com;
    SqlConnection con = new SqlConnection("Data Source=MAJOR-DYNASTI;Initial Catalog=ESS;Integrated Security=True");
   con.Open();
    string a = comboBox_semester.SelectedText;
    string str = "SELECT  CourseName, TeacherName, RoomName FROM Course_teacher, RoomInfo where Semester= ORDER BY NEWID(); ";
    com = new SqlCommand(str, con);
    SqlDataReader reader = com.ExecuteReader();
    if (reader.HasRows)
    {

        Random random = new Random();
        var labels = new Label[] { label1, label2, label3, label4, label5, label6, label7, label8,label9,label10,label11,label12,label13,label14,label15,label16,
                                   label17,label18,label19,label20,label21,label22,label23,label24,label25,label26,label27,label28,label29,label30,label31,
                                    label32,label33,label34,label35,label36,label37,label38,label39,label40,label41,label42,label43,label44,label45,label46,label47,label48,
                                    label49,label50};
        label1.Text = ""; label2.Text = ""; label3.Text = ""; label4.Text = ""; label5.Text = ""; label6.Text = "";
        label7.Text = "";label8.Text = "";label9.Text = "";label10.Text = "";label11.Text = "";label12.Text = "";label13.Text = "";label14.Text = "";label15.Text = "";label16.Text = "";
                                   label17.Text = "";label18.Text = "";label19.Text = "";label20.Text = "";label21.Text = "";label22.Text = "";label23.Text = "";label24.Text = "";label25.Text = "";label26.Text = "";label27.Text = "";label28.Text = "";label29.Text = "";label30.Text = "";label31.Text = "";
                                   label32.Text = ""; label33.Text = ""; label34.Text = ""; label35.Text = ""; label36.Text = ""; label37.Text = ""; label38.Text = ""; label39.Text = ""; label40.Text = ""; label41.Text = ""; label42.Text = ""; label43.Text = ""; label44.Text = ""; label45.Text = ""; label46.Text = ""; label47.Text = ""; label48.Text = "";
                                    label49.Text = "";label50.Text = "";

        if (
            comboBox1.SelectedItem==null ||
            comboBox2.SelectedItem==null ||
            comboBox_semester.SelectedItem==null)
        {
            MessageBox.Show("Please Select Complete Detail");

        }
        else
        {
            while (reader.Read())
            {

                int randomNumber = random.Next(1, 50);

                labels[randomNumber].Text = String.Format("{0},\r\n{1},\r\n{2}", reader["CourseName"], reader["TeacherName"], reader["RoomName"]);
            }
        }
    }

我想查询这两行。

string a = comboBox_semester.SelectedText;
string str = "SELECT  CourseName, TeacherName, RoomName FROM Course_teacher, RoomInfo where Semester=(what should I need to write here) ORDER BY NEWID(););

请帮助

2 个答案:

答案 0 :(得分:1)

您需要将变量var nums = prompt("please enter the numbers you wish to calculate separated by a comma. eg 1,3").split(",").map(function(t){return parseFloat(t);}); var choice = prompt("what do you wish to do with the numbers: "+nums +" your choices are: add, subtract, multiply, divide").toLowerCase(); var things = { total: 0, getTotal: function getTotal() { return this.total; }, add: function add(a, b) { this.total = parseFloat(a + b); }, subtract: function add(a, b) { this.total = parseFloat(a - b); console.log(things.total); }, multiply: function add(a, b) { this.total = parseFloat(a * b); console.log(things.total); }, divide: function add(a, b) { this.total = parseFloat(a / b); console.log(things.total); }, }; document.write("<h3>Calculations</h3><br>"); document.write("<p>you have elected to "+choice +" the numbers "+nums[0]+" and "+nums[1]+"</p>"); document.write("<p>the answer is: " + things.getTotal()+"</p>"); switch(choice) { case 'add': things.add(nums[0],nums[1]); break; case 'multiply': things.multiply(nums[0],nums[1]); break; case 'subtract': things.subtract(nums[0],nums[1]); break; case 'divide': things.divide(nums[0],nums[1]); break; default: console.log("nothing entered!"); break; } 放入查询中,如下所示:

a

请注意,我稍微清理了一下您的查询,并介绍了parameterized queries,以防您不熟悉它们。

答案 1 :(得分:0)

这样做的最佳方法是看起来像这样:

fun something(first: Int = 0, second: Date): Date { ... }

something(Date()) // ERROR: Type mismatch: inferred type is java.util.Date but kotlin.Int was expected

然后,在下一行中,将一个Parameter添加到SqlCommand对象中,如下所示:

"Where Semester = @Semester"

这让SQLCommand对象知道用您从用户收到的值替换您的变量(我现在将其命名为@Semester)。