怎样在C#中调用SQL的存储过程? 怎样在C#中调用SQL的存储过程?本人尝试了几次均出现错误,希望那位高手帮忙指点,最好能有个实例,谢啦 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SqlCommand.CommandType=CommandType.StoredProcedure; Sqlconnection sqlcon = new Sqlconnection("........");SqlCommand sqlcmd = new Sqlcommand();sqlcmd.Connection = sqlcon;sqlcmd.CommandType = CommandType.StoredProcedure;sqlcmd.CommandText = " ";SqlParameter sqlPa = sqlCmd.Parameters.Add(" ",SqlDbType.VarChar,20);sqlPa.Value = " ";sqlcon.Open();try{ sqlCmd.ExcuteNonquey();}catch(Exception ex){ throw new Exception(ex.Message);}finally{ sqlcon.Close();} sqlcmd.CommandType = CommandType.StoredProcedure; sqlcmd.CommandText 放的是存储过程的名称 Sqlconnection sqlcon = new Sqlconnection("........"); SqlCommand sqlcmd = new Sqlcommand(); sqlcmd.Connection = sqlcon; sqlcmd.CommandType = CommandType.StoredProcedure; sqlcmd.CommandText = " "; sqlCmd.Parameters.Add(" ",SqlDbType.VarChar,20); sqlCmd.Parameters[0].Value = " "; sqlcon.Open(); try { sqlCmd.ExcuteNonquey(); } catch(Exception ex) { throw new Exception(ex.Message); } finally { sqlcon.Close(); } public void ADONetTran2() { SqlConnection conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=123;"); SqlCommand cmd = new SqlCommand(); try { using (System.Transactions.TransactionScope ts = new TransactionScope()) { cmd.CommandText = "Update Region Set RegionDescription=@UpdateValue where RegionID=@UpdateID"; cmd.CommandType = CommandType.Text; cmd.Connection = conn; conn.Open(); SqlParameter[] paras = new SqlParameter[]{ new SqlParameter ("@UpdateID",SqlDbType.Int,32), new SqlParameter ("@UpdateValue",SqlDbType .NChar,50)}; paras[0].Value = "2"; paras[1].Value = "Update Value12"; foreach (SqlParameter para in paras) { cmd.Parameters.Add(para); } cmd.ExecuteNonQuery(); cmd.CommandText = "insert into Region values(@InsertID,@InsertValue)"; cmd.CommandType = CommandType.Text; paras = new SqlParameter[]{ new SqlParameter ("@InsertID",SqlDbType.Int ,32), new SqlParameter ("@InsertValue",SqlDbType.NChar ,50)}; paras[0].Value = "8"; paras[1].Value = "Insert Value"; cmd.Parameters.Clear(); foreach (SqlParameter para in paras) { cmd.Parameters.Add(para); } cmd.ExecuteNonQuery(); //提交事务 ts.Complete(); } } catch { throw; } finally { conn.Close(); } } 大侠们,救救我把!!!!!!!在线等!!!!!!!! webBrowser2的问题发贴的? 菜鸟勿进: 【求一个组合算法】 问一个关于ArrayList的问题 C#2.0中的泛型类型的构造函数的声明语法如此的垃圾 大家进来看看我说得对不对 密码验证 问题,还是很简单,我却很迷糊 讨论有关VSS 6.0!!!!! C#熟手请进! 怎么控制dataGrid的单元格里面的文本自动换行? 如何在一个dos程序里打开一个文件并赋给一个变量? 用C#语言读取文本文件中的数据(关于文件的问题) (请高手帮忙!!!谢谢了) C#开发windows程序如何 进行分工
SqlCommand sqlcmd = new Sqlcommand();
sqlcmd.Connection = sqlcon;
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = " ";
SqlParameter sqlPa = sqlCmd.Parameters.Add(" ",SqlDbType.VarChar,20);
sqlPa.Value = " ";
sqlcon.Open();
try
{
sqlCmd.ExcuteNonquey();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
sqlcon.Close();
}
SqlCommand sqlcmd = new Sqlcommand();
sqlcmd.Connection = sqlcon;
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = " ";
sqlCmd.Parameters.Add(" ",SqlDbType.VarChar,20);
sqlCmd.Parameters[0].Value = " ";
sqlcon.Open();
try
{
sqlCmd.ExcuteNonquey();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
sqlcon.Close();
}
{
SqlConnection conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=123;");
SqlCommand cmd = new SqlCommand();
try
{
using (System.Transactions.TransactionScope ts = new TransactionScope())
{
cmd.CommandText = "Update Region Set RegionDescription=@UpdateValue where RegionID=@UpdateID";
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
conn.Open();
SqlParameter[] paras = new SqlParameter[]{
new SqlParameter ("@UpdateID",SqlDbType.Int,32),
new SqlParameter ("@UpdateValue",SqlDbType .NChar,50)};
paras[0].Value = "2";
paras[1].Value = "Update Value12"; foreach (SqlParameter para in paras)
{
cmd.Parameters.Add(para);
}
cmd.ExecuteNonQuery();
cmd.CommandText = "insert into Region values(@InsertID,@InsertValue)";
cmd.CommandType = CommandType.Text; paras = new SqlParameter[]{
new SqlParameter ("@InsertID",SqlDbType.Int ,32),
new SqlParameter ("@InsertValue",SqlDbType.NChar ,50)};
paras[0].Value = "8";
paras[1].Value = "Insert Value"; cmd.Parameters.Clear();
foreach (SqlParameter para in paras)
{
cmd.Parameters.Add(para);
} cmd.ExecuteNonQuery();
//提交事务
ts.Complete();
}
}
catch
{
throw;
}
finally
{
conn.Close();
} }