C# 使用sql向数据集插入新记录
我花了几天时间试图让它工作并检查了大量教程,但它仍然无法工作。我遇到构建错误并且它工作正常,但是新行没有加载到要保存到数据库的数据集中。数据显示在我的 datagridview 中,但没有更新数据集。
我使用的是 sql express 2008 数据库,
虽然我是 c# 新手,但我在编程方面表现一般,这只是我的头脑,是否有人能指出我正确的方向或告诉我我做错了什么。感谢您的帮助。
private void button1_Click(object sender, EventArgs e)
{
string AccountName, Address, PhoneNumber, Suburb, Email, PostcodeInput;
int Postcode;
bool Parsed;
AccountName = textBox1.Text;
Address = textBox2.Text;
PhoneNumber = textBox5.Text;
Suburb = textBox3.Text;
PostcodeInput = postcodeTextBox.Text;
Email = textBox6.Text;
ToolsPlusDataSet ToolsDS = new ToolsPlusDataSet();
Parsed = Int32.TryParse(PostcodeInput, out Postcode);
if (!Parsed)
{
MessageBox.Show("Postcode was Invalid - Must be a numerical value");
NewAccount newaccount = new NewAccount();
newaccount.Show();
this.Close();
}
ToolsPlusDataSet.AccountDetailsRow newAccountDetail;
newAccountDetail = ToolsDS.AccountDetails.NewAccountDetailsRow();
newAccountDetail.AccountName = AccountName;
newAccountDetail.Address = Address;
newAccountDetail.PhoneNumber = PhoneNumber;
newAccountDetail.Suburb = Suburb;
newAccountDetail.Postcode = Postcode;
newAccountDetail.Email = Email;
ToolsDS.AccountDetails.Rows.Add(newAccountDetail);
this.accountDetailsTableAdapter.Update(ToolsDS.AccountDetails);
this.Close();
}
i have spent the few days trying to get this to work and checked a tonne of tutorials but it still won't work. i get go build errors and it works perfectly, but the new row isn't loading into the dataset to be saved to the database. the data shows up in my datagridview but isnt updating the dataset.
im using a sql express 2008 database
im average at programming though im new to c# and this is just wrapping my head around could some one either point me in the right direction or tell me what ive done wrong. thanks for the help.
private void button1_Click(object sender, EventArgs e)
{
string AccountName, Address, PhoneNumber, Suburb, Email, PostcodeInput;
int Postcode;
bool Parsed;
AccountName = textBox1.Text;
Address = textBox2.Text;
PhoneNumber = textBox5.Text;
Suburb = textBox3.Text;
PostcodeInput = postcodeTextBox.Text;
Email = textBox6.Text;
ToolsPlusDataSet ToolsDS = new ToolsPlusDataSet();
Parsed = Int32.TryParse(PostcodeInput, out Postcode);
if (!Parsed)
{
MessageBox.Show("Postcode was Invalid - Must be a numerical value");
NewAccount newaccount = new NewAccount();
newaccount.Show();
this.Close();
}
ToolsPlusDataSet.AccountDetailsRow newAccountDetail;
newAccountDetail = ToolsDS.AccountDetails.NewAccountDetailsRow();
newAccountDetail.AccountName = AccountName;
newAccountDetail.Address = Address;
newAccountDetail.PhoneNumber = PhoneNumber;
newAccountDetail.Suburb = Suburb;
newAccountDetail.Postcode = Postcode;
newAccountDetail.Email = Email;
ToolsDS.AccountDetails.Rows.Add(newAccountDetail);
this.accountDetailsTableAdapter.Update(ToolsDS.AccountDetails);
this.Close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我为 SQL Express 编写了这篇文章,使用了参数查询,如果有兴趣,您可以将其改编为您的应用程序。
http://code.msdn.microsoft.com/Esempio-applicazione-dati-494c129a问候
。
I wrote this article for SQL Express, with the use of parametric query, if interested you can adapt it to your application.
http://code.msdn.microsoft.com/Esempio-applicazione-dati-494c129a
Regards.