public bool UpdatePhone(string 姓名, string 身份证号)
{
using (System.Data.IDbConnection conn = DapperHelper.CreateLocalConnection())
{
if (string.IsNullOrEmpty(姓名) || 姓名 == "null")
{
return false;
}
List<string> listsql = new List<string>();
string sql = "update 表_a set 姓名='" + 姓名 + "' where 身份证号='" + 身份证号 + "'";
listsql.Add(sql);
string sqljiben = "update 表_b set 姓名='" + 姓名 + "' where 身份证号='" + 身份证号 + "'";
listsql.Add(sqljiben);
IDbTransaction transaction = conn.BeginTransaction();
try
{
for (int j = 0; j < listsql.Count; j++)
{
conn.Execute(listsql[j], null, transaction);
}
transaction.Commit();
return true;
}
catch (Exception exception)
{
transaction.Rollback();
return false;
}
//return conn.Execute(sql) > 0;
}
}
dapper链接数据库
public class DapperHelper
{
private static string GetConnectionString(string serverName)
{
return @"Data Source=.;Initial Catalog=test;Persist Security Info = True; User ID = sa; Password =123; Max Pool Size = 512;Connection Timeout=10000";
}
private static System.Data.IDbConnection CreateConnection(string serverName)
{
var connection = new System.Data.SqlClient.SqlConnection(GetConnectionString(serverName));
connection.Open();
return connection;
}
public static IDbConnection CreateLocalConnection()
{
return CreateConnection("localDb");
}
}