从WPF执行存储过程

时间:2012-10-11 07:22:22

标签: c# sql wpf stored-procedures

我正在做一个简单的wpf注册过程应用程序。用户提供的注册详细信息必须存储在服务器中。我想为此使用存储过程。我正在使用SQL Server 2008.我在服务器中创建了表和存储过程,用于存储来自用户的输入。我无法在我的wpf应用程序中使用它。以下是C#代码。

Registration.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Data; 

namespace storedprocedure
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class Registration : Window
{
    public Registration()
    {
        InitializeComponent();
    }
private void Login_Click(object sender, RoutedEventArgs e)
    {

        Login login = new Login();

        login.Show();

        Close();

    }



    private void button2_Click(object sender, RoutedEventArgs e)
    {

        Reset();

    }



    public void Reset()
    {

        textBoxFirstName.Text = "";

        textBoxLastName.Text = "";

        textBoxEmail.Text = "";

        textBoxAddress.Text = "";

        passwordBox1.Password = "";

        passwordBoxConfirm.Password = "";

    }

    private void button3_Click(object sender, RoutedEventArgs e)
    {

        Close();

    }



    private void Submit_Click(object sender, RoutedEventArgs e)
    {

        if (textBoxEmail.Text.Length == 0)
        {

            errormessage.Text = "Enter an email.";

            textBoxEmail.Focus();

        }

        else if (!Regex.IsMatch(textBoxEmail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
        {

            errormessage.Text = "Enter a valid email.";

            textBoxEmail.Select(0, textBoxEmail.Text.Length);

            textBoxEmail.Focus();

        }

        else
        {

            string firstname = textBoxFirstName.Text;

            string lastname = textBoxLastName.Text;

            string email = textBoxEmail.Text;

            string password = passwordBox1.Password;

            if (passwordBox1.Password.Length == 0)
            {

                errormessage.Text = "Enter password.";

                passwordBox1.Focus();

            }

            else if (passwordBoxConfirm.Password.Length == 0)
            {

                errormessage.Text = "Enter Confirm password.";

                passwordBoxConfirm.Focus();

            }

            else if (passwordBox1.Password != passwordBoxConfirm.Password)
            {

                errormessage.Text = "Confirm password must be same as password.";

                passwordBoxConfirm.Focus();

            }

            else
            {

                errormessage.Text = "";

                string address = textBoxAddress.Text;

                SqlConnection con = new SqlConnection("Data Source=DBSERVER\\DUBAIAIRPORT;Initial Catalog=LoginWPF;User ID=sa;Password=sa");


    con.Open(); 

    using (SqlCommand cmd = new SqlCommand("storedprocedure", con))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.ExecuteNonQuery(); 
    }

    con.Close();
}






            }
        }
    }

}

以下是我的sql server数据库中创建的存储过程

CREATE PROCEDURE submitdata
(
 @Firstname varchar(50),
 @Lastname varchar(50),
 @Email varchar(50),
 @Password varchar(50),
 @Address varchar(50)
 )
 AS
 insertinto                                                                              registrationdata(Firstname,Lastname,Email,Password,Address)values(@firstname,@lastname,               @email,@password,@address)

数据库的名称是storedprocedure。我正在使用VS 2010和.net4.0。

请帮助我必须在Registration.xaml.cs中插入的c#代码。 谢谢。

3 个答案:

答案 0 :(得分:3)

con.Open(); 

using (SqlCommand cmd = new SqlCommand("submitdata", con))
{
    cmd.CommandType = CommandType.StoredProcedure;

    SqlParameter param1 = new SqlParameter("@Firstname", SqlDbType.VarChar);
    param1.Value = "my first name";

     // ... the rest params

    cmd.Parameters.Add(param1);

    // cmd.Parameters.Add(param2);....

    cmd.ExecuteNonQuery(); 
}

con.Close();

答案 1 :(得分:1)

前几个问题
你尝试做什么?你有数据库连接吗?您正在使用什么样的技术来访问您的数据层(LINQ到某些东西?实体?根本没有抽象?)

您也应该发布您的实际代码。

代码示例
无论如何,这里有一个小代码示例,用于解释如何使用普通System.Data.SqlClient类执行存储过程。

private static void SubmitData(string firstName, string lastName)
{
    using (var connection = new SqlConnection("your connection string"))
    {
        connection.Open();

        using (var command = connection.CreateCommand())
        {
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "submitdata";

            command.Parameters.AddWithValue("@FirstName", firstName);
            command.Parameters.AddWithValue("@LastName", lastName);

            command.ExecuteNonQuery();
        }
    }
}

答案 2 :(得分:0)

如果在Visual Studio服务器资源管理器中连接到数据库,并创建LINQ to SQL类,则永远不必触摸SqlCommand。您的存储过程将作为可以调用的方法生成。

http://msdn.microsoft.com/en-us/library/bb386976.aspx

相关问题