我正在使用 Visual Studio 和 MSSMS 中的 Windows 应用程序表单界面进行演示,将数据插入“crystaldb”本地数据库以创建 Crystal 报表。我检查并假设我引用的 sql 数据库中有正确的表,并且它肯定被称为“crystaldb”。我第一次编译成功插入了1行数据,但之后编译时抛出sql异常。
Here's the interface:
Here's the code for the "Submit" and "Close" buttons and main function:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=ZPCU0209\\SQLSERVERYANG;Initial Catalog=crystaldb;Integrated Security=True");
con.Open();
SqlCommand sc = new SqlCommand("Insert into cystaldb values('"+ textBox1.Text +"',"+ textBox2.Text +",'"+ textBox3.Text +"','"+ textBox4.Text +"','"+ textBox5.Text +"','"+ textBox6.Text +"','"+ textBox7.Text +"','"+ textBox8.Text +"','"+ textBox9.Text +"','"+ textBox10.Text +"',"+ textBox11.Text +");",con);
int o= sc.ExecuteNonQuery();
MessageBox.Show(o + " : Record has been inserted");
con.Close();
}
public static void main(string[] args)
{
Application.Run(new Form1());
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
This is the database table setting:
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Invalid object name 'cystaldb'.
有人可以帮我解决这个问题吗?
最佳答案
您在查询中拼写错误 crystaldb:
SqlCommand sc = new SqlCommand("Insert into **cystaldb** values(...));
您缺少 crystaldb 中的“r”。
关于c# - 使用WFA接口(interface)插入数据时出现sqlException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42536129/