【发布时间】:2019-06-28 16:17:44
【问题描述】:
我正在为我的公司开发一个用于资产管理的数据库应用程序。我没有大量使用 SQL 或 MS Access 进行数据库管理的经验。我正在开发一种解决方案,通过让其运行 SQL 命令,在 Visual Studio 中使用 C# 将数据添加到数据库中。我在一些文本框中工作以显示我的代码是否在不同的地方运行,并且我(我认为)将其缩小到我的 SQL,虽然我不确定,但我没有找到太多通过搜索了解Access和OleDb。
我已经更改了 SQL 代码的大写和措辞,以及逗号和引号,并在代码可能捕获我的错误的地方进行了烘焙。
private void SubmitButton_Click(object sender, EventArgs e)
{
try
{
//declares connection
OleDbConnection con = new OleDbConnection();
OleDbCommand command = new OleDbCommand();
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\eric.varner\Documents\AssetDB.accdb";
//open connection to Database
con.Open();
StatusLabel.Text = "Connected";
//declares command type
command.Connection = con;
//SQL commands to call database to write data.
if (AssetTypeBox.Text == "iPad")
{
command.CommandText = "INSERT INTO AssetsiPad (Asset Tag, Condition, Location, Serial Number) VALUES('" + AssetBox.Text + "','" + ConditionBox.Text +
"','" + LocationBox.Text + "','" + SerialNumBox.Text + "')";
MessageBox.Show("The if statement runs fine");
}
else if (AssetTypeBox.Text == "iPhone")
{
command.CommandText = "INSERT INTO AssetsiPhone (Asset Tag, Condition, Location, Serial Number) VALUES('" + AssetBox.Text + "','" + ConditionBox.Text +
"','" + LocationBox.Text + "','" + SerialNumBox.Text + "')";
}
else if (AssetTypeBox.Text == "MR AP")
{
command.CommandText = "INSERT INTO AssetsMR (Asset Tag, Condition, Location, Serial Number, MAC Address, IP Address) VALUES('" + AssetBox.Text + "','"
+ ConditionBox.Text + "','" + LocationBox.Text + "','" + SerialNumBox.Text + "','" + MACaddressBox.Text + "','" + IPsubnetBox.Text + "')";
}
else if (AssetTypeBox.Text == "MX Security")
{
command.CommandText = "INSERT INTO AssetsMX (Asset Tag, Condition, Location, Serial Number) VALUES('" + AssetBox.Text + "','" + ConditionBox.Text + "','"
+ LocationBox.Text + "','" + SerialNumBox.Text + "',)";
}
else if (AssetTypeBox.Text == "Laptop")
{
command.CommandText = "INSERT INTO AssetsLaptop (Asset Tag, Condition, Location, Serial Number) VALUES('" + AssetBox.Text + "','" + ConditionBox.Text + "','"
+ LocationBox.Text + "','" + SerialNumBox.Text + "',)";
}
else
{
MessageBox.Show("you aren't reaching the correct statement");
}
command.ExecuteNonQuery();
//close connection to Database
con.Close();
MessageBox.Show("Data Saved");
}
catch (Exception ex)
{
StatusLabel.Text = "Not Connected";
MessageBox.Show("your sql didn't run correctly");
}
}
当我正确输入字符串(例如“iPad”)时,我会看到消息框显示“if 语句运行正常”和“您的 SQL 没有正确运行”。 AssetTypeBox 是我唯一内置任何类型的捕获器的东西。其他字段应该能够毫无问题地接受任何类型或数量的数据。我希望我不会遗漏任何东西。
【问题讨论】:
-
欢迎您!实际的问题是什么?您描述的行为似乎与您的代码匹配,因此请清楚地描述它有什么问题,以及您期望什么行为。如果是关于
Your SQL didn't run correctly,我建议您删除消息框,然后重新引发异常,或者至少显示异常消息。目前,您捕获所有异常并将其替换为通用消息框,因此您(或我们)不知道实际出了什么问题。 -
我希望通过将我输入的内容添加到文本框中来让代码编辑我的数据库。我的 SQL 命令没有运行,但我希望它们运行。非常感谢。
-
将
MessageBox.Show("your sql didn't run correctly");更改为MessageBox.Show(string.Format("{0}:\r\n{1}",ex.Message,ex.StackTrace));以提供更详细的错误描述。 -
出于调试目的,显示异常当然是最简单的,但最好实现一些日志记录(例如记录到文件)。这样,您就可以向用户显示您想要的所有好消息,同时在您的日志文件中提供所有细节,以备不时之需。