方法一:
string SQLString="select 1; select 2;";
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
using (OdbcCommand cmd = new OdbcCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (OdbcException e)
{
connection.Close();
throw e;
}
}
}
方法二:
List<string> sqls = new List<string>();
sqls.Add("select 1;");
sqls.Add("select 2;");
using (OdbcConnection conn = new OdbcConnection(connectionString))
{
conn.Open();
OdbcCommand cmd = new OdbcCommand();
cmd.Connection = conn;
OdbcTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
int count = 0;
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n];
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
count += cmd.ExecuteNonQuery();
}
}
tx.Commit();
return count;
}
catch
{
tx.Rollback();
return 0;
}
}
个人认为第二种方法较合适。