C#条件加入两个表

时间:2014-05-05 08:13:31

标签: c# asp.net

使用下面的代码后,我没有在gridview上获得任何输出,我没有正确连接表

int x = Convert.ToInt32(this.Request.QueryString["outageID"]);
int y = Convert.ToInt32(this.Request.QueryString["toolid"]);
SqlConnection con = new SqlConnection("xyz");//connection name
con.Open();
SqlCommand cmd = new SqlCommand("select * from tblOutageTransactionDetails,tbltools where  ToolsOutageID = " + x + "and toolid = " + y, con);

cmd.CommandType = CommandType.Text;

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds);
GridView1.DataSource = ds.Tables;
GridView1.DataBind();
con.Close();

1 个答案:

答案 0 :(得分:0)

由于Soner正确地说您的信息有限,但是如果您的查询出现异常,请尝试在and之前留一个空格,并且一次只从1个表中选择:

SqlCommand cmd = new SqlCommand("select * from tblOutageTransactionDetails where  ToolsOutageID = " + x + " and toolid = " + y, con);