private void button3_Click(object sender, EventArgs e)
{
DbCnn.con.Open();
SqlTransaction MyTra = DbCnn.con.BeginTransaction();
SqlCommand MyCom = new SqlCommand();
MyCom.Connection = DbCnn.con;
MyCom.Transaction = MyTra;
try
{
string gu = Guid.NewGuid().ToString().Trim();
MyCom.CommandText = “insert into user_info(no,name,psd,sex,type,clas,part,tel,id_card,read_time,zc_date,lend_no) values(“” + no.Text.Trim() + “”,”” + name.Text.Trim() + “”,”” + psd.Text.Trim() + “”,”” + sex.Text.Trim() + “”,”” + type.Text.Trim() + “”,”” + clas.Text.Trim() + “”,”” + part.Text.Trim() + “”,”” + tel.Text.Trim() + “”,”” + id_card.Text.Trim() + “”,”” + read_time.Text.Trim() + “”,”” + zc_date.Text.Trim() + “”,”” + lend_no.Text.Trim() + “”); insert into user_score(no,score) values(“” + no.Text.Trim() + “”,”” + lend_no.Text.Trim() + “”)”;
MyCom.ExecuteNonQuery();
MyTra.Commit();
MessageBox.Show(“成功执行”);
}
catch (Exception )
{
MyTra.Rollback();
MessageBox.Show(“添加失败”);
{
DbCnn.con.Open();
SqlTransaction MyTra = DbCnn.con.BeginTransaction();
SqlCommand MyCom = new SqlCommand();
MyCom.Connection = DbCnn.con;
MyCom.Transaction = MyTra;
try
{
string gu = Guid.NewGuid().ToString().Trim();
MyCom.CommandText = “insert into user_info(no,name,psd,sex,type,clas,part,tel,id_card,read_time,zc_date,lend_no) values(“” + no.Text.Trim() + “”,”” + name.Text.Trim() + “”,”” + psd.Text.Trim() + “”,”” + sex.Text.Trim() + “”,”” + type.Text.Trim() + “”,”” + clas.Text.Trim() + “”,”” + part.Text.Trim() + “”,”” + tel.Text.Trim() + “”,”” + id_card.Text.Trim() + “”,”” + read_time.Text.Trim() + “”,”” + zc_date.Text.Trim() + “”,”” + lend_no.Text.Trim() + “”); insert into user_score(no,score) values(“” + no.Text.Trim() + “”,”” + lend_no.Text.Trim() + “”)”;
MyCom.ExecuteNonQuery();
MyTra.Commit();
MessageBox.Show(“成功执行”);
}
catch (Exception )
{
MyTra.Rollback();
MessageBox.Show(“添加失败”);
}
finally
{
DbCnn.con.Close();
}
}
解决方案
40
没啥用,谁来了也是告诉你,没有添加DbCnn这个,你复制的代码没有复制全,这个是数据库定义的类。
给你的代码添加上
System.Data.SqlClient.SqlConnection;
string connectString = “Data Source=aa;Initial Catalog=bb;User ID=cc;Pwd=dd”;//本人改成你本人
SqlConnection sqlCnt = new SqlConnection(connectString);
//sqlCnt.Open();
把
DbCnn.con.Open();
SqlTransaction MyTra = DbCnn.con.BeginTransaction();
SqlCommand MyCom = new SqlCommand();
MyCom.Connection = DbCnn.con;
MyCom.Transaction = MyTra;
改成
sqlCnt.Open();
SqlTransaction MyTra = sqlCnt.BeginTransaction();
SqlCommand MyCom = new SqlCommand();
MyCom.Connection = sqlCnt;
MyCom.Transaction = MyTra;
finally
{
DbCnn.con.Close();
}
改成
finally
{
sqlCnt.Close();
}
复制了代码得知道是干什么用的