将数据从C#表单插入链接数据库?

时间:2018-04-09 17:47:35

标签: c# sql

我正在使用C#构建应用程序,并决定使用Windows窗体。目标是让用户注册并能够使用他们在注册表中设置的登录凭据进行登录。

报名表:

private void button1_Click(object sender, EventArgs e)
{
     string connetionString = null;
     connetionString = @"Data Source=THANATOS\SQLEXPRESS01;Initial Catalog=LoginDatabase;Integrated Security=True";

     SqlConnection cnn = new SqlConnection(connetionString);

     SqlCommand cmd = new SqlCommand();
     cmd.Connection = cnn;

     cmd.Parameters.AddWithValue("@FirstName", first_NameTextBox.Text);
     cmd.Parameters.AddWithValue("@LastName", last_NameTextBox.Text);
     cmd.Parameters.AddWithValue("@Username", usernameTextBox.Text);
     cmd.Parameters.AddWithValue("@Password", passwordTextBox.Text);
     cmd.Parameters.AddWithValue("@EmailAddress", email_AddressTextBox.Text);
     cmd.Parameters.AddWithValue("@PhoneNumbers", passwordTextBox.Text);

     cmd.CommandText = ("INSERT INTO UserRegiatration VALUES @FirstName, @LastName, @Username, @Password, @EmailAddress, @PhoneNumbers)");

     try
     {
         cnn.Open();
         cmd.ExecuteNonQuery();
         MessageBox.Show("You Have Been Registered!");
         cnn.Close();
     }
     catch (System.Exception ex)
     {
         MessageBox.Show("Please Try Again At A Later Time.");
     }
}

这是我到目前为止所得到的,但是当按钮点击事件发生时,我无法获取用户在文本框中输入的信息以发布到数据库。

知道我需要包含或删除哪些内容来改善这一点吗?

3 个答案:

答案 0 :(得分:0)

您错过了(

INSERT into UserRegiatration VALUES 
(@FirstName,@LastName,@Username,@Password,@EmailAddress,@PhoneNumbers)
^

另外,您的意思是写UserRegistration还是真正称为UserRegiatration的表?

答案 1 :(得分:0)

在某些情况下,您的表名应该用括号括起来。

看起来像是:

INSERT into [UserRegiatration] VALUES 
(@FirstName,@LastName,@Username,@Password,@EmailAddress,@PhoneNumbers)

答案 2 :(得分:0)

我重写了我的表并做了一些更多的研究,事实证明,我错过了声明中确定应该插入信息的特定列的部分。这就是现在为我工作的东西。感谢您的指导和故障排除建议。

private void registerButton_Click(object sender, EventArgs e)
    {
        string connetionString = null;
        connetionString = @"Data Source=THANATOS\SQLEXPRESS01;Initial Catalog=LoginDatabase;Integrated Security=True";                       
        try
        {
            using (SqlConnection connection = new SqlConnection(connetionString))
            {
                if (connection.State == ConnectionState.Closed) // Checking connection status, if closed then open.
                {
                    connection.Open();
                    this.Hide();
                }
                String query = "INSERT INTO dbo.[Table] (FirstName,LastName,Username,Password,Email,PhoneNum) VALUES (@FirstName,@LastName,@Username,@Password,@Email,@PhoneNum)";
                using (SqlCommand cmd = new SqlCommand(query, connection))
                {                       

                    cmd.Parameters.AddWithValue("@FirstName", firstNameTextBox.Text); // Syntax @"TableColumnName", TextBoxToGrabInfoFrom.Text
                    cmd.Parameters.AddWithValue("@LastName", lastNameTextBox.Text); // Syntax @"TableColumnName", TextBoxToGrabInfoFrom.Text
                    cmd.Parameters.AddWithValue("@Username", usernameTextBox.Text); // Syntax @"TableColumnName", TextBoxToGrabInfoFrom.Text
                    cmd.Parameters.AddWithValue("@Password", passwordTextBox.Text); // Syntax @"TableColumnName", TextBoxToGrabInfoFrom.Text
                    cmd.Parameters.AddWithValue("@Email", emailTextBox.Text); // Syntax @"TableColumnName", TextBoxToGrabInfoFrom.Text
                    cmd.Parameters.AddWithValue("@PhoneNum", phoneNumTextBox.Text); // Syntax @"TableColumnName", TextBoxToGrabInfoFrom.Text

                    int result = cmd.ExecuteNonQuery();

                    // Check Error
                    if (result < 0)
                        MessageBox.Show("Error inserting data into Database!"); // If error, display message.
                }
            }
        }
        catch (Exception ex)
        {
            string v = ex.Message;
            throw ex;
        }

    }

    private void phoneNumLabel_Click(object sender, EventArgs e)
    {

    }
}

}