C#业务类和存储过程(插入,更新,删除) [英] C# Business Class and Stored procedures (insert, update, delete)

查看:229
本文介绍了C#业务类和存储过程(插入,更新,删除)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些代码,但他们似乎没有插入值



I have these codes but they don''t seem to insert the values

[DataObjectMethod(DataObjectMethodType.Insert)]
    public static void InsertClass(string name, string code)
    {
        String strConnString = ConfigurationManager.ConnectionStrings["BookstoreAppConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "InsClass";

        cmd.Parameters.AddWithValue("@name", name);
        cmd.Parameters.AddWithValue("@code", code);
        //cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = txtname.Text.Trim();
        //cmd.Parameters.Add("@code", SqlDbType.VarChar).Value = txtcode.Text.Trim();
        cmd.Connection = con;
        
            con.Open();
            cmd.ExecuteNonQuery();
            //lblMessage.Text = "Record inserted successfully";
     
        
            con.Close();

    }





[以下评论更新:]



存储过程是:





[update from comments below:]

the stored procedure is:

ALTER procedure [dbo].[InsClass]
(
@name varchar(50),
@code varchar(50)
)
as 
begin
set nocount on;
insert into Class(ClassName, ClassCode)
values(@name, @code)
end

推荐答案

作为解决方案发布,以便我们关闭它:



看起来OP正在将null值传递给他的函数,并且需要在尝试插入SQL之前检查Fred。
Posted as a solution so we can close this down:

It looks like the OP was passing null values into his function and needs to check for this as Fred says before attempting to insert into SQL.


这篇关于C#业务类和存储过程(插入,更新,删除)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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