含有参数变量的sql语句,如何用C#语句表示。
private void button1_Click(object sender, EventArgs e){
string mystr;
SqlConnection myconn = new SqlConnection();
DataSet myds = new DataSet();
mystr = "Data Source =.;Initial Catalog = school;Integrated Security = True";
myconn.ConnectionString = mystr;
myconn.Open();
string mysql = "SELECT Cname,Grade FROM student,score,course WHERE student.Sno = score.Sno" +
"and score.Cno = course.Cno and student.Sno =" + Convert.ToString(textBox1.Text) +
"and score.Text =" + Convert.ToInt32(comboBox1.Text);
SqlDataAdapter myda = new SqlDataAdapter(mysql, myconn);
myda.Fill(myds, "school");
myconn.Close();
DataView mydv = new DataView(myds.Tables["school"]);
listBox1.Items.Add("课程名\t\t\t\t成绩");
for (int i = 0; i < mydv.Count; i++)
{
listBox1.Items.Add(String.Format("{0}\t\t\t{1}", mydv[i]["Cname"],
mydv[i]["Grade"]));
}
}
程序在运行时没有结果显示在listbox1中,能帮我看看错哪了。 C# sql 变量 --------------------编程问答-------------------- sql语句的问题
string mysql = "SELECT Cname,Grade FROM student,score,course WHERE student.Sno = score.Sno" +
" and score.Cno = course.Cno and student.Sno =" + Convert.ToString(textBox1.Text) +
" and score.Text =" + Convert.ToInt32(comboBox1.Text);
--换行后 and 的前面应该是空格 --------------------编程问答-------------------- 拼接有问题
"and score.Cno = course.Cno and student.Sno =" + Convert.ToString(textBox1.Text)
这里 你不妨断定看一看 你整句的sql 是不是在这里 student.sno= 后面的其实是个字符换 但是 在数据库里 应该是'字符串' 这样的格式 你应该是报语法错误的问题 --------------------编程问答-------------------- 建议改成 "and score.Cno = course.Cno and student.Sno = '" + Convert.ToString(textBox1.Text)+"'" 这样 --------------------编程问答-------------------- 这一串拼接多难看啊,去msdn看下sqlParameter用法
http://social.msdn.microsoft.com/Search/zh-CN?query=sqlParameter&ac=2
[code=sql]
补充:.NET技术 , C#