簡體   English   中英

從WPF執行存儲過程

[英]Execute stored procedure from WPF

我正在做一個簡單的WPF注冊過程的應用程序。 用戶提供的注冊詳細信息必須存儲在服務器中。 我想為此使用存儲過程。 我正在使用SQL Server2008。我在服務器中創建了表和存儲過程,用於存儲來自用戶的輸入。 我無法在我的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)

數據庫名稱為存儲過程。 我正在將VS 2010與.net4.0一起使用。

請幫助我在Registration.xaml.cs中插入的C#代碼。 謝謝。

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();

前幾個問題
你想做什么? 您有數據庫連接嗎? 您正在使用哪種技術訪問數據層(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();
        }
    }
}

如果您在Visual Studio服務器資源管理器中連接到數據庫,並創建LINQ to SQL類,則SqlCommand觸摸SqlCommand 您的存儲過程將作為一種可以調用的方法生成。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM