c#调用存储过程查询表并返回影响的行数

时间:2022-03-16 07:19:24

// 在此处放置用户代码以初始化页面        

String DBConnStr;        

DataSet MyDataSet = new DataSet();        

SqlDataAdapter DataAdapter = new SqlDataAdapter();        

DBConnStr = "server=192.168.2.120,2433;database=DB_test;uid=sa;pwd=43g";        

SqlConnection myConnection = new SqlConnection(DBConnStr);        

if (myConnection.State != ConnectionState.Open)

{            

myConnection.Open();        

}        

SqlCommand myCommand = new SqlCommand("Proc_test", myConnection);        

myCommand.CommandType = CommandType.StoredProcedure;         //添加输入查询参数、赋予值        

myCommand.Parameters.Add("@userInput", SqlDbType.VarChar);        

myCommand.Parameters["@userInput"].Value = "zhang,zhangjun";

//添加输出参数        

myCommand.Parameters.Add("@Rowcount", SqlDbType.Int);       

myCommand.Parameters["@Rowcount"].Direction = ParameterDirection.Output;

myCommand.ExecuteNonQuery();    

DataAdapter.SelectCommand = myCommand;

if (MyDataSet != null)       

{     

DataAdapter.Fill(MyDataSet, "table");       

}

//得到存储过程输出参数        

string retcount = myCommand.Parameters["@Rowcount"].Value.ToString();

if (myConnection.State == ConnectionState.Open)   

{         

myConnection.Close();        

}

drop proc Proc_test @userInput varchar(2000), @Rowcount INT OUTPUT as

if(@userInput = ‘‘)   begin     RAISERROR (‘输入项不能为空.‘, 17, 1)     RETURN   end

declare @sql varchar(4000);

set @userInput = ‘‘‘‘ + REPLACE(@userInput,‘,‘,‘‘‘,‘‘‘) + ‘‘‘‘

set @sql=‘select * from view_Shop_Order where _name in(‘+@userInput+‘)‘ set @Rowcount=1 print @sql; Exec(@sql)

Proc_test ‘zhang,zhangjun‘,0