如何通过WPF中的存储过程更新SQL数据库? [英] How do I update SQL Database via stored procedures in WPF?

查看:94
本文介绍了如何通过WPF中的存储过程更新SQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我解释我的问题时,请多多包涵.我认为这会更有意义.谢谢:

我在SQL Server 2005中有一个表,该表具有三列CustomerID(整数,身份),Name(varchar(50)和Address(varchar(50)).我为该表创建了四个存储过程(CustomerSelect,CustomerInsert,CustomerDelete和CustomerUpdate).

在VS 2008中,我使用Settings.settings创建了到表的连接字符串.然后,我使用以下代码创建了一个数据访问类,以使我能够销售并查看数据库中的客户:

Please bear with me while i explain my question. I think it will make more sense. Thank you:

I have a table in SQL server 2005 with three columns CustomerID (integer, identity), Name(varchar(50) and Address(varchar(50). I created four stored procedures for the table (CustomerSelect, CustomerInsert, CustomerDelete and CustomerUpdate).

In VS 2008, i created a connection string to the table using Settings.settings. I then created a Data Access Class with the following code to enable me sellect and view a customer in the database:

public class CustomerDAL
    {
        private string connectionString = Properties.Settings.Default.BranchAppConnString;

        public Branch SelectCustomer(int ID)
        {
            SqlConnection conn = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("CustomerSelect", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@CustomerID", ID);


            try
            {
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);

                if (reader.Read())
                {
                    Customer customer = new Customer((string)reader["customerName"], (string)reader["address"]);

                    return (customer);
                }

                else
                {
                    return null;
                }

            }

            finally
            {
                conn.Close();
            }
        }




现在,我使用客户实体的属性创建了数据类.我还使用部分类向应用程序公开了CustomerDAL类.现在,这很好用.我可以在文本框中插入一个客户ID,然后单击一个botton以查看该客户.

我的问题是:如何通过相同的存储过程使用相同的模式来插入,删除和更新客户表?

请帮我解决一下这个.谢谢




Now i created Data Class with the properties for the Customer Entity. I also exposed the CustomerDAL class to the application using a partial class. Now this works just fine. I can insert a Customer ID in a text box and click a botton to view the customer.

My question is: How can i use the same pattern to insert, delete and update the customer table via the corresponding stored procedures?

Please help me with this. Thanks

推荐答案

我不确定我是否正确理解了您的问题.
但是,正如我所见,您无需进行太多更改.只需使用正确的参数调用相应的存储过程即可.

要获得更好的主意,请参见此处.
I dont know for sure that I understand your question correctly.
But as I see it, you don''t need to change much. Just call the appropriate stored procedure with the right parameters.

To get a better idea, see here.


这篇关于如何通过WPF中的存储过程更新SQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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