gpt4 book ai didi

c# - 使用 Visual Studio 形式将信息输入数据库

转载 作者:行者123 更新时间:2023-11-30 00:34:28 25 4
gpt4 key购买 nike

当我尝试将信息输入到连接到 Visual Studio 2012 表单的数据库时遇到问题。当我开始调试时,如果我在字段中输入信息并点击提交,则不会将任何内容输入到我的数据库中。如果我再次输入信息并点击提交,该信息将写入我的数据库。我不太擅长编程,所以我使用数据源来链接我的 Visual Studio 表单和数据库。

这就是我的表单的样子; data form before entry

这就是我输入的数据的表单的样子; data form with information

这就是您点击提交时发生的情况; confirmation message

这是第一次点击提交后的数据库; db entries after first submission

这就是我再次输入信息时发生的情况; db entries after second submission

此后的任何条目都将正确输入,一切都很棒。我很困惑为什么信息(除了空条目之外)直到我第二次单击“提交”时才会输入。虽然它“技术上可行”,但并不完全正确。任何有关此问题的帮助将不胜感激。

这是我的新客户表单中的提交按钮的代码;

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
}

关于c# - 使用 Visual Studio 形式将信息输入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22287849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com