日期:2014-05-19  浏览次数:20951 次

数据库连接问题!急!大家帮帮小弟~初学者!
private   void   button1_Click(object   sender,   System.EventArgs   e)

string   str   =   @ "server=415-7E9290C5828;uid= " ";pwd= " ";database=deom "   ;

SqlConnection   conn   =   new   SqlConnection(str);
string   sql   =   "select   NAME   from   deom ";
SqlCommand   cmd   =   new   SqlCommand(sql,   conn);
conn.Open();
        SqlDataReader   myDataRead   =   cmd.ExecuteReader();
string[]   name=new   string[10];
int   i   =   0;
while   (myDataRead.Read())
{
name[i]   =   myDataRead[ "NAME "].ToString();
i++;
}
for   (i   =   0;   i   <name.Length;   i++)
{
textBox1.Text   =textBox1.Text   +name[i]   +   "   ";
                               
}
conn.Close();
}

为什么说的myDataRead没有定义呢?

------解决方案--------------------
string sql = "select NAME from deom "; //一般是从表里select 不是数据库啊

string sql = "select NAME from table ";
------解决方案--------------------
这样操作比较简单


SqlConnection conn = new SqlConnection(str);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(ds, "TEST ");
if (ds.Tables[0].Rows.Count!=0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
textBox1.Text += ds.Tables[0].Rows[i][ "NAME "].ToString() + " ";

}
}