两个有什么不同?为什么结果不同呢?请指点一下!
1.string thiscncode = cn_SoftCodeTextBox.Text.Trim();
OleDbConnection thisconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + "/CnEduCode.mdb");
OleDbCommand mycomm = new OleDbCommand();
mycomm.Connection = thisconn;//初始化connection
= "select Cn_SoftCode from Cn_SoftWare where Cn_SoftCode ='" + thiscncode + "'";
thisconn.Open();//打开连接
OleDbDataReader thisread = mycomm.ExecuteReader();
if (thisread.Read())
{
MessageBox.Show("软件代号已存在,请重新输入!");
thisread.Close();
return;
}
else
{
string article = "insert into Cn_SoftWare (Cn_SoftName,Cn_Remark,Cn_SoftCode) values('" + cn_SoftNameTextBox.Text + "','" + cn_RemarkTextBox.Text + "' ,'" + cn_SoftCodeTextBox.Text + "')";
OleDbCommand comm = new OleDbCommand(article, thisconn);//创建Command命令对象
comm.ExecuteNonQuery();//执行命令
MessageBox.Show("添加完成!");
}
databind();
thisconn.Close();//关闭连接
return;
2.
string thiscncode = cn_SoftCodeTextBox.Text.Trim();
OleDbConnection thisconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + "/CnEduCode.mdb");
OleDbCommand mycomm = new OleDbCommand();
mycomm.Connection = thisconn;//初始化connection
= "select Cn_SoftCode from Cn_SoftWare where Cn_SoftCode ='" + thiscncode + "'";
thisconn.Open();//打开连接
OleDbDataReader thisread = mycomm.ExecuteReader();
if (thisread.Read())
{
MessageBox.Show("软件代号已存在,请重新输入!");
thisread.Close();
return;
}
else
{
string article = "insert into Cn_SoftWare (Cn_SoftName,Cn_Remark,Cn_SoftCode) values('" + cn_SoftNameTextBox.Text + "','" + cn_RemarkTextBox.Text + "' ,'" + cn_SoftCodeTextBox.Text + "')";
OleDbCommand comm = new OleDbCommand(article, thisconn);//创建Command命令对象
comm.ExecuteNonQuery();//执行命令
databind();
MessageBox.Show("添加完成!");
}
thisconn.Close();//关闭连接
return;
databind函数是
private void databind()
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + "/CnEduCode.mdb");
OleDbDataAdapter sda = new OleDbDataAdapter("select * from Cn_SoftWare", con);
DataSet ds = new DataSet();
sda.Fill(ds, "Cn_SoftWare");
this.dataGridView1.DataSource = ds.Tables["Cn_SoftWare"];
}
一个可以即时显示,一个不可,为什么呢?
先谢了!