Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将具有相同主键的两个表SQL Server连接到C#_C#_Mysql_Sql Server - Fatal编程技术网

将具有相同主键的两个表SQL Server连接到C#

将具有相同主键的两个表SQL Server连接到C#,c#,mysql,sql-server,C#,Mysql,Sql Server,怎么了,伙计们,我在这个代码上遇到了一些问题,我希望你们能解决 我有两个使用SQL Server的表(客户和注册) 顾客 身份证 名字 等 登记 身份证 日期 我正在编写我的C#程序,我想将它们显示在列表视图中,但我有一个错误,这是我的代码: public void listdata() { SqlDataReader reader = null; listView1.Items.Clear(); listView1.Columns.Cl

怎么了,伙计们,我在这个代码上遇到了一些问题,我希望你们能解决

我有两个使用SQL Server的表(客户和注册)

顾客

  • 身份证
  • 名字
  • 登记

  • 身份证
  • 日期
  • 我正在编写我的C#程序,我想将它们显示在
    列表视图中
    ,但我有一个错误,这是我的代码:

    public void listdata()
        {
            SqlDataReader reader = null;
    
            listView1.Items.Clear();
            listView1.Columns.Clear();
            listView1.Columns.Add("ID", 55, HorizontalAlignment.Center);
            listView1.Columns.Add("Tanggal Registrasi", 150, HorizontalAlignment.Center);
            listView1.Columns.Add("Nama Pemohon", 150, HorizontalAlignment.Center);
    
            System.Data.SqlClient.SqlConnection conn = konn.GetConn();
            try
            {
                conn.Open();
                string sql = "select*from Ms_Register a join Ms_Coba b on a.id = b.id where id='" + textBox1.Text + "'";
                SqlCommand command = new SqlCommand(sql, conn); 
                command.ExecuteNonQuery();
                conn.Close();
    
                //Check
                reader = command.ExecuteReader();
    
    
                while (reader.Read())
                {
                    ListViewItem item1 = new     ListViewItem(reader["id"].ToString(), 0);
                    item1.SubItems.Add(reader["tanggal"].ToString());
                    item1.SubItems.Add(reader["nama"].ToString());
    
                    listView1.Items.Add(item1);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                conn.Close();
            }
    
        }
    
    两个表具有相同的id,错误为“不明确的列名”id“”
    有人有办法解决这个问题吗?谢谢

    您的查询错误,请按以下方式更改:

    string sql = "select * from Ms_Register a join Ms_Coba b on a.id = b.id where a.id='" + textBox1.Text + "'";
    
    因为这两个表包含相同的字段名,所以我们必须提到在where子句上选择哪个表字段

    select * from Ms_Register a join Ms_Coba b on a.id = b.id where id='" + textBox1.Text + "'"
    
    应该是
    其中a.id
    其中b.id
    ,在这种情况下哪个无关紧要,因为它们是相同的

    这是非常糟糕的代码,不过,您应该通过使用查询参数来清理textBox1的值。您的代码易受SQL注入攻击


    看起来您的两个表都有
    id
    列,读者会混淆您想要获取哪个
    id
    。您是否尝试过使用完整的数据库和列名获取它?顺便说一句,
    select*
    通常是个坏主意。您是按客户还是按注册进行筛选?将其更改为“where a.id=””或“where b.id=”。我使用您的代码,答案是:Execute reader需要一个开放且可用的连接。连接当前状态为关闭。怎么了?您在运行command.ExecuteOnQuery()后关闭了连接,这就是读取器要求您重新打开连接的原因。最好在while循环完成后关闭连接。那么您是否认为不可能在2表中获取具有相同id的数据?不,这是可能的,并且上面的答案是正确的。