C# 使用visual studio窗体将信息输入数据库
当我尝试将信息输入到连接到visual studio 2012表单的数据库时,遇到了一个问题。当我开始调试时,如果我在字段中输入信息并点击submit,则不会在数据库中输入任何内容。如果我再次输入信息并点击提交,信息将写入我的数据库。我并不擅长编程,所以我使用了数据源来链接我的VisualStudio表单和数据库 这就是我的身体看起来的样子; 这就是我输入的数据的形式; 这就是当你点击提交时发生的事情; 这是第一次点击提交后的数据库 这就是我再次输入信息时发生的情况 在此之后的任何条目都将正确输入,一切都很好。我很困惑,为什么除了空条目之外的信息在我第二次单击submit时才会输入。虽然技术上可行,但并不完全正确。在此问题上的任何帮助都将不胜感激 这是我在新客户表单中提交按钮的代码C# 使用visual studio窗体将信息输入数据库,c#,mysql,winforms,visual-studio-2012,C#,Mysql,Winforms,Visual Studio 2012,当我尝试将信息输入到连接到visual studio 2012表单的数据库时,遇到了一个问题。当我开始调试时,如果我在字段中输入信息并点击submit,则不会在数据库中输入任何内容。如果我再次输入信息并点击提交,信息将写入我的数据库。我并不擅长编程,所以我使用了数据源来链接我的VisualStudio表单和数据库 这就是我的身体看起来的样子; 这就是我输入的数据的形式; 这就是当你点击提交时发生的事情; 这是第一次点击提交后的数据库 这就是我再次输入信息时发生的情况 在此之后的任何条目都将
namespace WindowsFormsApplication2
{
public partial class frmNewCustomer : Form
{
public frmNewCustomer()
{
InitializeComponent();
}
private void btnAddCustomer_Click(object sender, EventArgs e)
{
string cstFName;
string cstLName;
string cstAddress;
string cstCity;
string cstState;
string cstZip;
string cstPhone;
string cstEmail;
cstFName = cstFNameTxtBox.Text;
cstLName = cstLNameTxtBox.Text;
cstAddress = cstAddressTxtBox.Text;
cstCity = cstCityTxtBox.Text;
cstState = cstStateTxtBox.Text;
cstZip = cstZipTxtBox.Text;
cstPhone = cstPhoneTxtBox.Text;
cstEmail = cstEmailTxtBox.Text;
// exception handler for empty fields
if (cstFName == "")
{
MessageBox.Show("Please enter your first name!");
}
else if (cstLName == "")
{
MessageBox.Show("Please enter your last name!");
}
else if (cstAddress == "")
{
MessageBox.Show("Please enter your address!");
}
else if (cstCity == "")
{
MessageBox.Show("Please enter your city!");
}
else if (cstState == "")
{
MessageBox.Show("Please enter your state!");
}
else if (cstZip == "")
{
MessageBox.Show("Please enter your zip!");
}
else if (cstPhone == "")
{
MessageBox.Show("Please enter your Phone!");
}
else if (cstEmail == "")
{
MessageBox.Show("Please enter your email!");
}
else
{
MessageBox.Show("First Name: " + cstFName + " Last Name: " + cstLName + "\r\n" +
"Address: " + cstAddress + "\r\n" +
"City: " + cstCity + " State: " + cstState + " Zip: " + cstZip + "\r\n" +
"Phone: " + cstPhone + " Email: " + cstEmail + "\r\n" + "\r\n" +
"Has been added to the database.");
}
this.tblCustomersBindingSource.AddNew();
this.tblCustomersBindingSource.EndEdit();
this.tblCustomersTableAdapter.Update(this.dataSet1.tblCustomers); // Updating the DB Table
}
private void frmNewCustomer_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dataSet1.tblCustomers' table. You can move, or remove it, as needed. Loads it into start of form!
// this.tblCustomersTableAdapter.Fill(this.dataSet1.tblCustomers);
}
}
}
我意识到我的异常处理充其量也很简单,但这更多的是关于创建一些表单来读写数据库中的数据。我感谢任何人能够提供的任何帮助,如果这不重要,winforms不确定这是否重要,典型的流程是:
Create a connection.
Write your insert statement.
Do data validation.
Submit to database.
Commit transaction.
试试这个:
bool shouldSubmit = false;
// exception handler for empty fields
if (cstFName == "")
{
MessageBox.Show("Please enter your first name!");
}
else if (cstLName == "")
{
MessageBox.Show("Please enter your last name!");
}
else if (cstAddress == "")
{
MessageBox.Show("Please enter your address!");
}
else if (cstCity == "")
{
MessageBox.Show("Please enter your city!");
}
else if (cstState == "")
{
MessageBox.Show("Please enter your state!");
}
else if (cstZip == "")
{
MessageBox.Show("Please enter your zip!");
}
else if (cstPhone == "")
{
MessageBox.Show("Please enter your Phone!");
}
else if (cstEmail == "")
{
MessageBox.Show("Please enter your email!");
}
else
{
shouldSubmit = true;
MessageBox.Show("First Name: " + cstFName + " Last Name: " + cstLName + "\r\n" +
"Address: " + cstAddress + "\r\n" +
"City: " + cstCity + " State: " + cstState + " Zip: " + cstZip + "\r\n" +
"Phone: " + cstPhone + " Email: " + cstEmail + "\r\n" + "\r\n" +
"Has been added to the database.");
}
if(shouldSubmit)
{
this.tblCustomersBindingSource.AddNew();
this.tblCustomersBindingSource.EndEdit();
this.tblCustomersTableAdapter.Update(this.dataSet1.tblCustomers); // Updating the DB Table
}