C#中使用帶傳回值的預存程序

來源:互聯網
上載者:User
2007-01-27 08:37 P.M.

例如在向資料庫添加新資料時,需要檢測是否有重複
本例介紹如何把這個檢測的過程放在預存程序中,並用程式調用檢測的結果做出反應。
預存程序如下:

CREATE PROCEDURE DInstitute_Insert
@InstituteNO nvarchar(6),@InstituteName nvarchar(40)
 AS
declare @return int,@count int
if(ltrim(rtrim(@InstituteName))='' or ltrim(rtrim(@InstituteNO))='')
 select @return=3--返回3表示提交的資料有空值
else
begin
 select @count=count(1) from DInstitute where InstituteNO=@InstituteNO
 if(@count>0)
  select @return=1--返回1表示編號有重複
 else
 begin 
  insert into DInstitute (InstituteNO,InstituteName) values  (@InstituteNO,@InstituteName) 
  if(@@error>0)
   select @return=2--返回2表示資料操作錯誤
  else
   select @return=0--返回0表示資料操作成功
 end
end
return @return
GO

其中DInstitute 是一個學院資訊表。只有InstituteNO(學院編號)、InstituteName(學院名稱)兩個欄位。

在C#中調用本預存程序的代碼如下:

//執行插入操作
            SqlCommand com1 = new SqlCommand("DInstitute_Insert", DBcon);
            if (com1.Connection.State == ConnectionState.Closed)
                com1.Connection.Open();
            com1.CommandType = CommandType.StoredProcedure;
            com1.Parameters.Add(new SqlParameter("@InstituteNO",SqlDbType.NVarChar,6));
            com1.Parameters.Add(new SqlParameter("@InstituteName", SqlDbType.NVarChar, 40));
            com1.Parameters.Add(new SqlParameter("@return", SqlDbType.Int));
            com1.Parameters["@return"].Direction = ParameterDirection.ReturnValue;
            com1.Parameters["@InstituteNO"].Value = t_NO.Text;
            com1.Parameters["@InstituteName"].Value = t_name.Text;
            try
            {
                com1.ExecuteScalar();
            }
            catch(SqlException ee)
            {
                DB.msgbox("操作失敗!"+ee.Message.ToString());
                return;
            }
            finally
            {
                com1.Connection.Close();
            }
            string temp = com1.Parameters["@return"].Value.ToString();
            //返回0表示資料操作成功
            //返回1表示編號有重複   
            //返回2表示資料操作錯誤 
            //返回3表示提交的資料有空值
            switch (temp)
            {
                case "0":
                    DB.msgbox("添加成功!");
                    break;
                case "1":
                    DB.msgbox("編號有重複!");
                    break;
                case "2":
                    DB.msgbox("資料操作錯誤!");
                    break;
                case "3":
                    DB.msgbox("提交的資料有空值!");
                    break;
            }
            Binding(); //重新整理datagrid

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.