这个错误每次都会发生。我应该怎么办?
This error keeps happening every time. What should I do?
我已经开始着手现金券项目。当我单击“提交”按钮时,会出现一个弹出框并显示“无效对象 voucher_table”。
我该怎么办??
private void bunifuFlatButton1_Click(object sender, EventArgs e)
{
string con = ConfigurationManager.ConnectionStrings["mydb"].ConnectionString;
SqlConnection sqlcn = new SqlConnection(con);
sqlcn.Open();
try
{
SqlCommand cmd = new SqlCommand("INSERT INTO voucher_table(customerID, planName, days, planAmount, validFrom, validTo, amountInWords, date, rupees) values(@customerID, @planName, @days, @planAmount, @validFrom, @validTo, @amountInWords, @date, @rupees)", sqlcn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@customerID", customerID.Text);
cmd.Parameters.AddWithValue("@planName", planName.Text);
cmd.Parameters.AddWithValue("@days", days.Text);
cmd.Parameters.AddWithValue("@planAmount", planAmount.Text);
cmd.Parameters.AddWithValue("@validFrom", validFrom.Text);
cmd.Parameters.AddWithValue("@validTo", validTo.Text);
cmd.Parameters.AddWithValue("@amountInWords", amountInWordsTextBox1.Text + amountInWordsTextBox2.Text);
cmd.Parameters.AddWithValue("@date", date.Text);
cmd.Parameters.AddWithValue("@rupees", rupees.Text);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Voucher Created Successfully");
SqlCommand cmd1 = new SqlCommand("select max(primaryNo) from voucher_table", sqlcn);
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
MessageBox.Show("Your Voucher No is '" + dr1.GetInt32(0) + "'Your Voucher is Created Successfully!");
Voucher_Successful success = new Voucher_Successful();
success.ShowDialog();
}
this.Close();
}
else
{
MessageBox.Show("An Error Occured... Voucher Not Created");
}
sqlcn.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
编辑:Image 01
Image 02
编辑:找到答案:
我添加了 Initial Catalog:My Cash Voucher;在 app.config 中,问题已解决。
我认为您不在正确的数据库中(即 SQL 命令没有找到 voucher_table)。
检查 voucher_table 是否确实存在于您连接的数据库中。
我发现了一个与你类似的问题,你可能也会在那里找到帮助:SQL Server: invalid object name in query execution
我认为您的连接字符串中存在问题,因为错误消息清楚地提到找不到 table 名称。有一种检查连通性的简单方法。只需执行以下操作
无需添加连接,仅用于测试。测试完成后点击取消按钮将其关闭。
我已经开始着手现金券项目。当我单击“提交”按钮时,会出现一个弹出框并显示“无效对象 voucher_table”。 我该怎么办??
private void bunifuFlatButton1_Click(object sender, EventArgs e)
{
string con = ConfigurationManager.ConnectionStrings["mydb"].ConnectionString;
SqlConnection sqlcn = new SqlConnection(con);
sqlcn.Open();
try
{
SqlCommand cmd = new SqlCommand("INSERT INTO voucher_table(customerID, planName, days, planAmount, validFrom, validTo, amountInWords, date, rupees) values(@customerID, @planName, @days, @planAmount, @validFrom, @validTo, @amountInWords, @date, @rupees)", sqlcn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@customerID", customerID.Text);
cmd.Parameters.AddWithValue("@planName", planName.Text);
cmd.Parameters.AddWithValue("@days", days.Text);
cmd.Parameters.AddWithValue("@planAmount", planAmount.Text);
cmd.Parameters.AddWithValue("@validFrom", validFrom.Text);
cmd.Parameters.AddWithValue("@validTo", validTo.Text);
cmd.Parameters.AddWithValue("@amountInWords", amountInWordsTextBox1.Text + amountInWordsTextBox2.Text);
cmd.Parameters.AddWithValue("@date", date.Text);
cmd.Parameters.AddWithValue("@rupees", rupees.Text);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Voucher Created Successfully");
SqlCommand cmd1 = new SqlCommand("select max(primaryNo) from voucher_table", sqlcn);
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
MessageBox.Show("Your Voucher No is '" + dr1.GetInt32(0) + "'Your Voucher is Created Successfully!");
Voucher_Successful success = new Voucher_Successful();
success.ShowDialog();
}
this.Close();
}
else
{
MessageBox.Show("An Error Occured... Voucher Not Created");
}
sqlcn.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
编辑:Image 01
Image 02
编辑:找到答案: 我添加了 Initial Catalog:My Cash Voucher;在 app.config 中,问题已解决。
我认为您不在正确的数据库中(即 SQL 命令没有找到 voucher_table)。 检查 voucher_table 是否确实存在于您连接的数据库中。
我发现了一个与你类似的问题,你可能也会在那里找到帮助:SQL Server: invalid object name in query execution
我认为您的连接字符串中存在问题,因为错误消息清楚地提到找不到 table 名称。有一种检查连通性的简单方法。只需执行以下操作
无需添加连接,仅用于测试。测试完成后点击取消按钮将其关闭。