ExecuteNonQuery:Connection属性尚未初始化 20
publicstaticSqlConnectionsqlconn;privatestaticvoidConnDatabase()//连接数据库{stringconstri...
public static SqlConnection sqlconn;
private static void ConnDatabase()//连接数据库
{
string constring = "Data source=.;database=PSMS;uid=sa;pwd=123456";
SqlConnection sqlconn = new SqlConnection(constring);
sqlconn.Open();
}
private void button1_Click_1(object sender, EventArgs e)
{
string str1 = this.groupBox1.Text;//这里是获得控件文本的TEXT的值
string str2 = this.groupBox2.Text;//这里是获得控件文本的TEXT的值
string SqlInsert = "insert into Employees(employeeID,Name,Title,Army,Sex,WorkYear,Address,PhoneNumber,DepartmentID,Working,tID) values(" + "\'" + this.textBox1.Text + "\',\'" + this.textBox2.Text + "\' ,\'" + this.textBox3.Text + "\' , ,\'" + this.textBox4.Text + "\' ,\'" + str1 + "\' ,\'" + this.textBox5.Text + "\' ,\'" + this.textBox6.Text + "\',\'" + this.textBox7.Text + "\',\'" + this.textBox8.Text + "\',\'" + str2 + "\' ,\'" + this.textBox9.Text + "\' )";
SqlCommand sqlcmd = new SqlCommand(SqlInsert, sqlconn);
sqlcmd.Connection = sqlconn;
sqlcmd.CommandText = SqlInsert;
try
{
int a = sqlcmd.ExecuteNonQuery();
if (a > 0)
{
MessageBox.Show("插入成功");
}
else
{
MessageBox.Show("插入失败");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "插入失败");
return;
}
SqlDataAdapter da = new SqlDataAdapter("select * from Employees", sqlconn);
DataSet ds = new DataSet();//采用 DataSet的方式也可以实现
da.Fill(ds, "Employees");
//采用 DataTable对象的方式也可以实现。两种方式都可以。
//DataTable tb = new DataTable("KCB");
//da.Fill (tb);
//dataGridView1.DataSource = tb;
}
insert里多了一个逗号,但是删掉后还是提示同样的错误~~大家帮帮忙、。 展开
private static void ConnDatabase()//连接数据库
{
string constring = "Data source=.;database=PSMS;uid=sa;pwd=123456";
SqlConnection sqlconn = new SqlConnection(constring);
sqlconn.Open();
}
private void button1_Click_1(object sender, EventArgs e)
{
string str1 = this.groupBox1.Text;//这里是获得控件文本的TEXT的值
string str2 = this.groupBox2.Text;//这里是获得控件文本的TEXT的值
string SqlInsert = "insert into Employees(employeeID,Name,Title,Army,Sex,WorkYear,Address,PhoneNumber,DepartmentID,Working,tID) values(" + "\'" + this.textBox1.Text + "\',\'" + this.textBox2.Text + "\' ,\'" + this.textBox3.Text + "\' , ,\'" + this.textBox4.Text + "\' ,\'" + str1 + "\' ,\'" + this.textBox5.Text + "\' ,\'" + this.textBox6.Text + "\',\'" + this.textBox7.Text + "\',\'" + this.textBox8.Text + "\',\'" + str2 + "\' ,\'" + this.textBox9.Text + "\' )";
SqlCommand sqlcmd = new SqlCommand(SqlInsert, sqlconn);
sqlcmd.Connection = sqlconn;
sqlcmd.CommandText = SqlInsert;
try
{
int a = sqlcmd.ExecuteNonQuery();
if (a > 0)
{
MessageBox.Show("插入成功");
}
else
{
MessageBox.Show("插入失败");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "插入失败");
return;
}
SqlDataAdapter da = new SqlDataAdapter("select * from Employees", sqlconn);
DataSet ds = new DataSet();//采用 DataSet的方式也可以实现
da.Fill(ds, "Employees");
//采用 DataTable对象的方式也可以实现。两种方式都可以。
//DataTable tb = new DataTable("KCB");
//da.Fill (tb);
//dataGridView1.DataSource = tb;
}
insert里多了一个逗号,但是删掉后还是提示同样的错误~~大家帮帮忙、。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询