如何解决“无效对象名”;表名“;在C#中

本文关键字:无效对象名 表名 对象 何解决 解决 无效 | 更新日期: 2023-09-27 18:19:29

当我在Visual Studio 2008中运行我的应用程序时,它会向我显示带有无效对象名"表名"符号的MessageBox,而表已经在我的数据库中了。所以请帮我解决这个问题。

private void FrmInwardDisp2_Load(object sender, EventArgs e)
{
  byte _true = 1;
  byte _false = 0;
  //this.toolTip1.SetToolTip(this.dgvDisplay, "Double Click to Edit");
  int width = Screen.PrimaryScreen.Bounds.Width;
  int height = Screen.PrimaryScreen.Bounds.Height - 175;
  dgvDisplay.Width = width - 25;
  dgvDisplay.Height = height;
  //dgvDisplay.AutoSize = true;
  try
  {
    SqlConnection con = new SqlConnection(Variables.con);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.SelectCommand = cmd;
    switch (Static_Class.selected)
    {
      case 1:
      {
         btnAddInward.Text = "Add Rental Inward";
         cmd.CommandText = "SELECT [RentalInwardNo],[VoucherNo],[NameOfWork]
         [Location],[NameOfVendor],[VendorCode],[VendorItemCode],[ItemName],
         [Narration],[WareHouse],[DriverName],[VehicleNo],[RejectedItemCode],  
         [RejectedItemName],[Rate],[Date],[InvoiceNo],[ChallanNo],[TenderCode],
         [PINo],[PONo],[UnitQty],[UOM],[RequisitionNo],[ReceivedBy],
         [LocationID],[Layer],[Local],[Global],[DeleteStatus],[AutoGenerated] 
         FROM IN_Rental_Inward 
         where [TenderCode]= " + Static_Class.tendercode + " 
         and [LocationID]='" + Static_Class.LocationID + "' 
         and [Layer]='" + Static_Class.Layer + "' 
         and [DeleteStatus]=" + 0;
         try
         {
           //con.Open();
           da.Fill(ds, "TableDisplay");
           da.Dispose();
           cmd.Dispose();
           //con.Close();
           dgvDisplay.DataSource = ds.Tables["TableDisplay"];
         }
         catch (Exception ex)
         {
           MessageBox.Show(ex.Message);
         }
      }
      break;

如何解决“无效对象名”;表名“;在C#中

在查询文本中,在表名称之前指定表的模式名称。例如:

cmd.CommandText = "SELECT … FROM dbo.IN_Rental_Inward …";
//                               ^^^^