如何解决C#中的无效对象名“表名”?

时间:2014-08-27 05:54:14

标签: c# sql-server

当我在Visual Studio 2008中运行我的应用程序时,它向我显示带有无效对象名称符号的消息框"表名称"而不是该表已存在于我的数据库中。所以请帮我解决这个问题。

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;

1 个答案:

答案 0 :(得分:-1)

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

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