if (button1.Text == "保存"&&textBox1 .Text !="")
{
try
{
con.Open();\\前面已声明静态字段CON
string insertcmd = "insert into TbRenyuan(User_Name,User_Phone,User_Email,Dept_Kename) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')"; OleDbCommand cmd = new OleDbCommand(insertcmd, con);
int i = cmd.ExecuteNonQuery();
if (i>0)
{
MessageBox.Show("保存成功");
}
button1.Text = "添加";
this.textBox1.Enabled = false;
this.textBox2.Enabled = false;
this.textBox3.Enabled = false;
this.textBox4.Enabled = false;
con.Close();
con.Dispose();
cmd.Dispose();
}
catch(Exception ex) { MessageBox.Show("错误:尚未保存成功"+ex.Message);}
}错误是connection属性尚未初始化!
高手帮忙解决下,小弟新手!
{
try
{
con.Open();\\前面已声明静态字段CON
string insertcmd = "insert into TbRenyuan(User_Name,User_Phone,User_Email,Dept_Kename) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')"; OleDbCommand cmd = new OleDbCommand(insertcmd, con);
int i = cmd.ExecuteNonQuery();
if (i>0)
{
MessageBox.Show("保存成功");
}
button1.Text = "添加";
this.textBox1.Enabled = false;
this.textBox2.Enabled = false;
this.textBox3.Enabled = false;
this.textBox4.Enabled = false;
con.Close();
con.Dispose();
cmd.Dispose();
}
catch(Exception ex) { MessageBox.Show("错误:尚未保存成功"+ex.Message);}
}错误是connection属性尚未初始化!
高手帮忙解决下,小弟新手!
没有的话要先 new OleDbConnection();
而且要指定连接字
声名的时候有没有new?
这样也可以
OleDbConnection myconn=new OleDbConnection();
myconn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=mydb.mdb;";
myconn.Open();
string strsql="select * from mytable";
OleDbCommand mycmd=new OleDbCommand(strsql,myconn);