为什么在VS窗体向sql存东西,运行一直报错: Fill: SelectCommand.Connection 属性尚未初始化。 5
privatevoidbutton2_Click(objectsender,EventArgse){SqlConectionconnection=newSqlConect...
private void button2_Click(object sender, EventArgs e)
{
SqlConection connection = new SqlConection("server=.;integrated security=true;database=XKXT");
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommandBuilder scb = new SqlCommandBuilder(adapter);
int id = int.Parse(textBox1.Text);
string name = textBox2.Text;
string classroom = textBox3.Text;
string teacher = textBox4.Text;
string time = textBox5.Text;
int credit = int.Parse(textBox7.Text);
string note = textBox8.Text;
int precourse = 0;
if (textBox6.Text != "")
precourse = int.Parse(textBox6.Text);
string queryString = "select * from C";
//MessageBox.show(queryString);
adapter.SelectCommand = new SqlCommand(queryString,Connection);
adapter.Fill(ds);
DataRow dr = ds.Tables[0].NewRow();
dr["ID"] = id;
dr["NAME"] = name;
dr["CLASSROOM"] = classroom;
dr["TEACHER"] = teacher;
dr["TIME"] = time;
dr["CREDIT"] = credit;
dr["NOTE"] = note;
if (precourse != 0)
dr["PRECOURSE"] = precourse;
ds.Tables[0].Rows.Add(dr);
adapter.Update(ds);
} 展开
{
SqlConection connection = new SqlConection("server=.;integrated security=true;database=XKXT");
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommandBuilder scb = new SqlCommandBuilder(adapter);
int id = int.Parse(textBox1.Text);
string name = textBox2.Text;
string classroom = textBox3.Text;
string teacher = textBox4.Text;
string time = textBox5.Text;
int credit = int.Parse(textBox7.Text);
string note = textBox8.Text;
int precourse = 0;
if (textBox6.Text != "")
precourse = int.Parse(textBox6.Text);
string queryString = "select * from C";
//MessageBox.show(queryString);
adapter.SelectCommand = new SqlCommand(queryString,Connection);
adapter.Fill(ds);
DataRow dr = ds.Tables[0].NewRow();
dr["ID"] = id;
dr["NAME"] = name;
dr["CLASSROOM"] = classroom;
dr["TEACHER"] = teacher;
dr["TIME"] = time;
dr["CREDIT"] = credit;
dr["NOTE"] = note;
if (precourse != 0)
dr["PRECOURSE"] = precourse;
ds.Tables[0].Rows.Add(dr);
adapter.Update(ds);
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询