程序如下:让它记录下我对它的理解吧!
class Program
{
static void Main(string[] args)
{
string conStr=@"Data Source=MICROSOF-E258B5\SQLSERVER2008;Initial Catalog=PB_BookShopDemo;Integrated Security=True";
string[] paraTran = new string[] { "d","fd","dfas","fafdf"};
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = con.CreateCommand())
{
con.Open();
SqlTransaction tran = con.BeginTransaction();
cmd.Transaction = tran;
try
{
for (int i = 0; i
string sql = "insert into T_Student(Name) values ('"+paraTran[i]+"')";//这里注意拼接sql语句!
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
tran.Commit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
tran.Rollback();
}
finally
{
con.Close();
Console.WriteLine("插入成功!");
Console.ReadKey();
}
}
}
}
}