怎样在C#中调用SQL的存储过程?本人尝试了几次均出现错误,希望那位高手帮忙指点,最好能有个实例,谢啦

解决方案 »

  1.   

    SqlCommand.CommandType=CommandType.StoredProcedure;
      

  2.   

    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();
    }
      

  3.   

    sqlcmd.CommandType = CommandType.StoredProcedure; sqlcmd.CommandText 放的是存储过程的名称
      

  4.   

    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(); 

      

  5.   

      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();
                }        }