请帮助我处理WPF中的存储过程插入 [英] Please Help me with Stored Procedure Inserts in WPF

查看:106
本文介绍了请帮助我处理WPF中的存储过程插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server 2005中创建了一个存储过程,应该将值插入到客户表中.在Visual Studio 2008中,我创建了一个表示客户表中字段的数据类.我还创建了一个数据访问类,该类接受连接到数据库以执行CRUD的操作.我能够从表中获取信息,执行更新和删除.

但是,我尝试插入记录数天,但没有成功.我相信麻烦不在于我的VS Application!下面是数据访问类插入方法的代码:

I created a stored procedure in SQL Server 2005 is supposed to insert values into a table, Customers. In Visual Studio 2008, I created a data class that represents the fields in the Customer Table. I also created a Data Access Class that accepts connects to the database to perform CRUD. I am able to fetch information from the table, perform updates and delete.

However, I have tried inserting records for days, without success. I believe the trouble is not from my VS Application! Below is the code for the Data Access Class Insert Method:

public void InsertCustomer(string customerName, string address)
        {
            SqlConnection conn = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("CustomerInsert", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@CustomerName", customerName);
            cmd.Parameters.AddWithValue("@Address", address);
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                return;
           }
            catch
            {
                conn.Close();
                return;
            }
        }



我在此方法中放置了一个断点,并且所有变量都完全保留了我想要插入到数据库中的内容.但是,没有任何东西可以进入数据库.

表定义如下:



I placed a break point in this method, and all the variables are holding exactly what i want inserted into the database. However nothing makes it into the database.

The Table definition is as follows:

CustomerID int, PK
CustomerName varchar(50)
Address varchar(50)





以下是存储过程代码:





Below is the Stored Procedure Code:

CREATE PROCEDURE [dbo].[CustomerInsert]
(
    @CustomerID   int,
    @CustomerName varchar(50),
    @Address    varchar(50)
	
)
AS
INSERT INTO dbo.Customer
(
    CustomerID,
    CustomerName,
    Address
)
VALUES
(
    @CustomerID,
    @CustomerName,
    @Address
)



请问我做错了什么?有人请帮助我!正在等待.....



Please what am i doing wrong? Someone PLEASE HELP ME!!!! Waiting.....

推荐答案

您没有设置@CustomerID参数.

如果运行代码,则会得到一个异常,告诉您确切的信息.

Nick
You''re not setting the @CustomerID parameter.

If you run your code you will get an exception that tells you exactly that.

Nick


从存储过程中删除CustomerId并检查是否为自动递增.
remove CustomerId from de store procedure and check that is auto increment.


从存储过程中删除CustomerId并检查为自动递增.

并为您添加一条消息,如


remove CustomerId from de store procedure and check that is auto increment.

And add a mesage for you like


try
{
....
}
catch(Exeption ex)
{
Messagebox.show(ex.toString(), "Error");
// or 
//Console.Writeln("Error: " + ex.ToString());
....
}


这篇关于请帮助我处理WPF中的存储过程插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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