C#Windows应用程序中的数据插入问题 [英] Data insertion problem in C# windows application
本文介绍了C#Windows应用程序中的数据插入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我单击注册"按钮时,它显示诸如过程或函数sp_InsertStudent"的错误,期望参数"@RollNo"(未提供).我没有找到解决方法...请帮助.
When i click the Register button it gives error like "Procedure or Function ''sp_InsertStudent'' expects parameter ''@RollNo'', which was not supplied.". I m not finding how to solve it... Plz help.
//code for front end that works against the clicking of Register button
private void btnRegister_Click(object sender, EventArgs e)
{
string source = ("data source = localhost; database = student; " +
"Integrated Security = SSPI");
SqlConnection conn = new SqlConnection(source);
conn.Open();
SqlCommand cmd = new SqlCommand("sp_InsertStudent", conn);
cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.Add("@RollNo", SqlDbType.Int, 0, "RollNo");
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 30, "Name");
cmd.Parameters.Add("@CourseName", SqlDbType.VarChar, 20,
"CourseName");
cmd.Parameters.Add("@Fee", SqlDbType.VarChar, 6,
"CourseName");
cmd.ExecuteNonQuery();
conn.Close();
}
-- My stored Procedure for inserting data in the table Student_Registration
Create Procedure sp_InsertStudent
@RollNo int,
@Name Varchar(30),
@CourseName Varchar(20),
@Fee int
As
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.Student_Registration
(
RollNo,
Name,
CourseName,
Fee
)
VALUES
(
@RollNo,
@Name,
@CourseName,
@Fee
)
END
GO
[修改:添加的代码格式]
[Modified: added code formatting]
推荐答案
ajitinbang写道:
cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.Add("@RollNo,SqlDbType.Int,0," RollNo);
cmd.Parameters.Add("@ Name",SqlDbType.VarChar,30,"Name");
在我看来,应该提供RollNo参数的行已被注释掉.
ajitinbang wrote:
cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.Add("@RollNo", SqlDbType.Int, 0, "RollNo");
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 30, "Name");
Looks to me like the line that should supply the RollNo parameter is commented out.
我认为您有一些错误.
1.您需要将值传递给SP
2.您在@fee上输入的类型错误(应为int)
尝试使用AddWithValue方法.
I think you have a few things wrong.
1. you need values to pass to the SP
2. your type on @fee is wrong (should be int)
Try using the AddWithValue method.
private void btnRegister_Click(object sender, EventArgs e)
{
//Need Variables to add to SP
int RollNumber=1;
string Name="Some Name";
string courseName="Course Name";
int Fee=25;
string source = ("data source = localhost; database = student; " +
"Integrated Security = SSPI");
SqlConnection conn = new SqlConnection(source);
conn.Open();
SqlCommand cmd = new SqlCommand("sp_InsertStudent", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@RollNo",RollNumber);
cmd.Parameters.AddWithValue("@Name",Name);
cmd.Parameters.AddWithValue("@CourseName",courseName);
cmd.Parameters.AddWithValue("@Fee",Fee);
cmd.ExecuteNonQuery();
conn.Close();
}
这篇关于C#Windows应用程序中的数据插入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文