首页 > 解决方案 > 这个错误每次都会发生。我该怎么办?

问题描述

我已经开始从事现金券项目。当我单击提交按钮时,会出现一个弹出框并显示“无效对象凭证表”。我该怎么办??

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);
        }

编辑:图片 01

图片 02

编辑:找到的答案:我添加了初始目录:我的现金券;在 app.config 中,问题已解决。

标签: c#sqlsql-serverado.net

解决方案


我认为您不在正确的数据库中(即 SQL 命令找不到凭证表)。检查您连接的数据库中是否确实存在凭证表。

我发现了一个与你类似的问题,你也可以在那里找到帮助:SQL Server: invalid object name in query execution


推荐阅读