当前位置: 代码迷 >> Sql Server >> select * from tb_command;这句话检索的是主键吗,该怎么解决
  详细解决方案

select * from tb_command;这句话检索的是主键吗,该怎么解决

热度:19   发布时间:2016-04-24 09:13:54.0
select * from tb_command;这句话检索的是主键吗

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace WindowsFormsApplication14
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection conn;
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection("server=.;database=db_15;uid=sa;pwd=");
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select *  from tb_command ";
            cmd.CommandType = CommandType.Text;
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                listView1.Items.Add(sdr[1].ToString());
            }
            conn.Dispose();
            button1.Enabled = false;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

其中cmd.CommandText = "select *  from tb_command ";检索的是主键元素吗?为什么返回的是姓名?数据库表如下

------解决思路----------------------


SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                listView1.Items.Add(sdr[1].ToString());
            }

下标从 0 开始 , 

select * , 是查询出所有的列,你查显示编号,要写成 sdr[0] , 
  相关解决方案