我正在尝试制作一个将数据形式存储到 Sql 的程序。我最初是通过从我的表单中进行插入查询来完成这部分的,并且工作得很好,但是我想使用 sql 存储过程使我的代码更健壮并且程序更快。我的 SQL 中的过程代码如下(这是来自 alter procedure 选项的代码):
USE [DBStudent]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[shto_student_proc]
@result int output,
@emer_std [nvarchar](50),
@atesi_std [nvarchar](50) ,
@mbiemer_std [nvarchar](50) ,
@datelindje_std [nvarchar](50) ,
@dok_std [nvarchar](50) ,
@nrdok_std [nvarchar](50),
@adresa_std [nvarchar](50) ,
@telefon_std [nvarchar](50),
@kategori_std [nvarchar](50) ,
@aktiv_std [nvarchar](2) ,
@email_std [nvarchar](50) ,
@menaxher_std nvarchar(2)
AS
BEGIN
SET NOCOUNT ON;
SELECT @result=COUNT(*) from dbo.student
WHERE st_emer=@emer_std and st_mbiemer=@mbiemer_std and st_datelindje=@datelindje_std and st_dok=@dok_std
-- check if record exists
IF @result=0
BEGIN
INSERT INTO student ([st_emer],[st_atesi],[st_mbiemer],[st_datelindje],[st_dok],[st_nrdok],[st_adresa],[st_telefon],[st_kategoria],st_aktiv,[st_email],st_menaxher)
VALUES (@emer_std,@atesi_std,@mbiemer_std ,@datelindje_std,@dok_std,@nrdok_std ,@adresa_std,@telefon_std,@kategori_std,@aktiv_std,@email_std,@menaxher_std )
END
END
GO
这是按钮的 C# 代码:
private void save_btn_Click(object sender, EventArgs e)
{
string date=dtl_dtp.Value.ToString("yyyyMMdd");
string komand_procedura = " EXECUTE dbo.shto_student_proc @emer_std,@atesi_std,@mbiemer_std ," +
"@datelindje_std,@dok_std,@nrdok_std ,@adresa_std,@telefon_std,@kategori_std,@aktiv_std,@email_std,@menaxher_std ";
SqlCommand cmd = new SqlCommand(komand_procedura,con1);
cmd.Parameters.Add(new SqlParameter("@emer_std",SqlDbType.NVarChar,50));
cmd.Parameters["@emer_std"].Value = emer_tb.Text;
cmd.Parameters.Add(new SqlParameter("@atesi_std",SqlDbType.NVarChar,50));
cmd.Parameters["@atesi_std"].Value = atesi_tb.Text;
cmd.Parameters.Add(new SqlParameter("@mbiemer_std",SqlDbType.NVarChar,50));
cmd.Parameters["@mbiemer_std"].Value = mbiemer_tb.Text;
cmd.Parameters.Add(new SqlParameter("@datelindje_std",SqlDbType.NVarChar,50));
cmd.Parameters["@datelindje_std"].Value = date;
cmd.Parameters.Add(new SqlParameter("@dok_std",SqlDbType.NVarChar,50));
cmd.Parameters["@dok_std"].Value = dok_cmb.Text;
cmd.Parameters.Add(new SqlParameter("@nrdok_std",SqlDbType.NVarChar,50));
cmd.Parameters["@nrdok_std"].Value = nrdok_tb.Text;
cmd.Parameters.Add(new SqlParameter("@adresa_std",SqlDbType.NVarChar,50));
cmd.Parameters["@adresa_std"].Value = textBox5.Text;
cmd.Parameters.Add(new SqlParameter("@telefon_std",SqlDbType.NVarChar,50));
cmd.Parameters["@telefon_std"].Value = textBox6.Text;
cmd.Parameters.Add(new SqlParameter("@kategori_std",SqlDbType.NVarChar,50));
cmd.Parameters["@kategori_std"].Value = comboBox1.Text;
cmd.Parameters.Add(new SqlParameter("@aktiv_std",SqlDbType.NVarChar,2));
cmd.Parameters["@aktiv_std"].Value="PO";
cmd.Parameters.Add(new SqlParameter("@email_std",SqlDbType.NVarChar,50));
cmd.Parameters["@email_std"].Value = textBox7.Text;
//MessageBox.Show(cmd.Parameters["@email_std"].Value.ToString());
cmd.Parameters.Add("@menaxher_std", SqlDbType.NVarChar, 2);
cmd.Parameters["@menaxher_std"].Value="JO";
SqlParameter output = cmd.Parameters.Add("@result",SqlDbType.Int);
output.Direction = ParameterDirection.ReturnValue;
con1.Open();
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Added "+output.ToString()+" rows");
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
con1.Close();
}
我有这个错误:
过程或函数“shto_student_proc”需要参数“@menaxher_std”,但未提供该参数。
我尝试使用文本框来检查值是否已添加到参数中,并且它们是。谁能帮我?