数据未插入数据库 [英] Data is not inserted into the database

查看:62
本文介绍了数据未插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试将数据插入数据库.
插入按钮的代码是

我也给了连接字符串.

Hi,
I''m Trying to insert the data into the DataBase.
Code For insert button is

I gave the connection string also.

 try
{
    cmd.Parameters.Clear();
    cmd.CommandText = "Ins_Upd_Students";
    cmd.Parameters.AddWithValue("@Sno", textBox1.Text);
    cmd.Parameters.AddWithValue("@Sname", textBox2.Text);
    cmd.Parameters.AddWithValue("@Class", textBox3.Text);
    cmd.Parameters.AddWithValue("@Fees", textBox4.Text);
    MessageBox.Show("Executed");
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    con.Close();
}



我的存储过程是



My Stored Procedure for this is

ALTER Procedure [dbo].[Ins_Upd_Students](@Sno Int,@Sname varchar(50),@Class Int,@Fees Money)
As
Begin
If Not Exists(Select Sno From Students Where Sno=@Sno)
Insert Into Stu(Sno,Sname,Class,Fees)
Values(@Sno,@Sname,@Class,@Fees)
Else
Update Stu Set Sname=@Sname,Class=@Class,Fees=@Fees Where Sno=@Sno
End



请遵守我的代码,并告诉我该代码有任何问题

谢谢你



Please Observe my code and tell me does any thing is problem in this code

Thanks u alot

推荐答案

您没有执行该命令.对于除"Select .."语句之外的所有语句,都应使用ExecuteNonQuery.

You''re not executing the command. for every but a "Select.." statement you should use ExecuteNonQuery.

int affectedRecord = cmd.ExecuteNonQuery();


你会把这个放在
之后


you''ll put this after

cmd.Parameters.AddWithValue("@Fees", textBox4.Text);


这篇关于数据未插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆