C# 使用存储过程检查ASP.NET中的插入是否成功
我知道这可能是个老问题,但我对使用存储过程获取“受影响的行数”感到困惑 这是我的密码:C# 使用存储过程检查ASP.NET中的插入是否成功,c#,sql-server,stored-procedures,insert,C#,Sql Server,Stored Procedures,Insert,我知道这可能是个老问题,但我对使用存储过程获取“受影响的行数”感到困惑 这是我的密码: public DataSet saveuser(string procedure, string username, string password) { SqlConnection connection = new SqlConnection(); SqlCommand command = new SqlCommand(procedure, connection); command
public DataSet saveuser(string procedure, string username, string password)
{
SqlConnection connection = new SqlConnection();
SqlCommand command = new SqlCommand(procedure, connection);
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);
SqlAdapter adapter = new SqlAdapter(command);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
connection.Close();
return dataset,
}
我尝试过以下代码:
int rowschanged = command.ExecuteNonQuery();
这似乎对我有帮助,但当我检查数据库插入时是两次
你知道吗?我可以使用
SqlDataAdapter
获取受影响的行数吗 “受影响的行数”是查询本身的返回,您所能做的是更改SP,使其在成功时返回0,或在未成功时返回-1(当然可以是任何其他值,您需要在SP代码中执行某种错误验证)“受影响的行数”是查询本身的返回,您可以做的是更改SP,如果成功,它将返回0;如果未成功,它将返回-1(当然可以是任何其他值,您需要在SP代码中执行某种类型的错误验证)我不确定您为什么返回数据集
如果您使用DataSet,这里的任何方法都是解决方案
public DataSet saveuser(string procedure, string username, string password)
{
SqlConnection connection = new SqlConnection();
SqlCommand command = new SqlCommand(procedure, connection);
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet dataset = new DataSet();
connection.Open();
adapter.Fill(dataset);
var count = dataset.Tables?[0].Rows.Count;
connection.Close();
return dataset;
}
如果您想使用DataTable
public DataTable SaveuserwithDataTable(string procedure, string username, string password)
{
SqlConnection connection = new SqlConnection();
SqlCommand command = new SqlCommand(procedure, connection);
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable dt = new DataTable();
connection.Open();
adapter.Fill(dt);
var count = dt.Rows.Count;
connection.Close();
return dt;
}
希望这有帮助 我不知道你为什么要返回数据集 如果您使用DataSet,这里的任何方法都是解决方案
public DataSet saveuser(string procedure, string username, string password)
{
SqlConnection connection = new SqlConnection();
SqlCommand command = new SqlCommand(procedure, connection);
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet dataset = new DataSet();
connection.Open();
adapter.Fill(dataset);
var count = dataset.Tables?[0].Rows.Count;
connection.Close();
return dataset;
}
如果您想使用DataTable
public DataTable SaveuserwithDataTable(string procedure, string username, string password)
{
SqlConnection connection = new SqlConnection();
SqlCommand command = new SqlCommand(procedure, connection);
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable dt = new DataTable();
connection.Open();
adapter.Fill(dt);
var count = dt.Rows.Count;
connection.Close();
return dt;
}
希望这有帮助 您应该签出并停止使用.AddWithValue()
-它可能会导致意外的结果…您应该签出并停止使用.AddWithValue()
-它可能会导致意外的结果。。。