接上篇……
//通过参数传递数据,将数据插入到本地数据库!
private void DataInOut(string Title, string ColumnsID, string Detail, string DateTime, string Audit, string UserName,string ReadNum)
{
SqlConnection mycon = new SqlConnection("server=localhost;uid=sa;pwd='密码';database=数据库名");
mycon.Open();
SqlTransaction mytrans = mycon.BeginTransaction();
// 为事务创建一个命令
SqlCommand mycmd = new SqlCommand("insert into 数据表名(Title,ColumnsID,Detail,DateTime,Audit,UserName,ReadNum) values(@Title, @ColumnsID, @Detail, @DateTime, @Audit, @UserName,@ReadNum)", mycon);
mycmd.Parameters.Add(new SqlParameter("@Title", SqlDbType.VarChar, 8000));
mycmd.Parameters["@Title"].Value = Title;
mycmd.Parameters.Add(new SqlParameter("@ColumnsID", SqlDbType.Int, 4));
mycmd.Parameters["@ColumnsID"].Value = ColumnsID;
mycmd.Parameters.Add(new SqlParameter("@Detail", SqlDbType.VarChar, 8000));
mycmd.Parameters["@Detail"].Value = Detail;
mycmd.Parameters.Add(new SqlParameter("@DateTime", SqlDbType.VarChar, 8000));
mycmd.Parameters["@DateTime"].Value = DateTime;
mycmd.Parameters.Add(new SqlParameter("@Audit", SqlDbType.VarChar, 8000));
mycmd.Parameters["@Audit"].Value = Audit;
mycmd.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar, 8000));
mycmd.Parameters["@UserName"].Value = UserName;
mycmd.Parameters.Add(new SqlParameter("@ReadNum", SqlDbType.Int, 4));
mycmd.Parameters["@ReadNum"].Value = ReadNum;
mycmd.Connection = mycon;
mycmd.Transaction = mytrans;
try
{
mycmd.ExecuteNonQuery();
mytrans.Commit();
}
catch (Exception ex)
{
mytrans.Rollback();
Response.Write(ex.ToString().Trim());
}
finally
{
mycon.Close();
}
}
}