后台-插件-广告管理-内容页广告位一(手机)

您现在的位置是:首页 > 开发类 > 问答问答

这段代码有问题吗?

2021-07-06 20:33:26问答人已围观

简介
public static int ExecuteNonQuery(out int id, DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandP

public static int ExecuteNonQuery(out int id, DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
{
if (connection == null) throw new ArgumentNullException("connection");
if (string.IsNullOrEmpty(Provider.GetLastIdSql().Trim())) throw new ArgumentNullException(" GetLastIdSql is Null Or Empty" );

// 创建DbCommand命令,并进行预处理
DbCommand cmd = Factory.CreateCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (DbTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);

// 执行命令
int retval = cmd.ExecuteNonQuery();//这里没问题
// 清除参数,以便再次使用.
cmd.Parameters.Clear();
cmd.CommandType = CommandType.Text;
cmd.CommandText = Provider.GetLastIdSql();


System.Diagnostics.Debug.Assert(cmd.ExecuteScalar()==null);//总是返回空
id = int.Parse(cmd.ExecuteScalar().ToString());

m_querycount++;


if (mustCloseConnection)
{
connection.Close();
}
return retval;
}

我运行一个insert语句,前面的运行良好,可是后面的取回自增值就报错了,返回的总是空值。郁闷来了。自己在外面写的时候好好的,放到这里面就不行了。

其它内部方法

private static void PrepareCommand(DbCommand command, DbConnection connection, DbTransaction transaction, CommandType commandType, string commandText, DbParameter[] commandParameters, out bool mustCloseConnection)
{
if (command == null) throw new ArgumentNullException("command");
if (string.IsNullOrEmpty(commandText)) throw new ArgumentNullException("commandText");

// If the provided connection is not open, we will open it
if (connection.State != ConnectionState.Open)
{
mustCloseConnection = true;
connection.Open();
}
else
{
mustCloseConnection = false;
}

// 给命令分配一个数据库连接.
command.Connection = connection;

// 设置命令文本(存储过程名或SQL语句)
command.CommandText = commandText;

// 分配事务
if (transaction != null)
{
if (transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
command.Transaction = transaction;
}

// 设置命令类型.
command.CommandType = commandType;

// 分配命令参数
if (commandParameters != null)
{
AttachParameters(command, commandParameters);
}
return;
}


public string GetLastIdSql()
{
return "SELECT SCOPE_IDENTITY()";
}

---

    //System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("insert into lcs_Sys_Model ([Model_GUID],[Model_Name],[Model_Desp],[Model_IsSys]) values ( 'F3CD1369-58C0-4A1F-AF88-05FCF734E079','name','desp',1 )", new System.Data.SqlClient.SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=officially;Data Source=LCSNB\SQL2K"));
//cmd.Connection.Open();
//cmd.ExecuteNonQuery();
//cmd.Parameters.Clear();
//cmd.CommandText = "SELECT SCOPE_IDENTITY()";
//Console.WriteLine(cmd.ExecuteScalar().ToString());
//cmd.Connection.Close();

这个没问题,他们有啥区别呢?看不出个所以然来。

最佳答案:

文章来源:https://q.cnblogs.com/q/2692/

Tags:.net技术 c 

很赞哦! ()

后台-插件-广告管理-内容页广告位二(手机)

相关文章

后台-插件-广告管理-内容页广告位三(手机)
后台-插件-广告管理-内容页广告位四(手机)

文章评论

留言与评论(共有 0 条评论)
   
验证码:

本栏推荐

站点信息

  • 文章统计90254篇文章
  • 浏览统计10087次浏览
  • 评论统计1个评论
  • 标签管理标签云
  • 统计数据:统计代码
  • 微信公众号:扫描二维码,关注我们