string connStr = "Data Source=HM;Initial Catalog=MyDB;Integrated Security=True";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
string sql = "select Name from Customer where (CustomerID = 'Hm')";
SqlCommand m_Command = new SqlCommand(sql, conn);
//CommandType m_CommandType = CommandType.Text;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(m_Command);
da.Fill(ds);
this.textBox1.Text=ds.Tables[0].DefaultView[0].ToString();
conn.Close();提示:
this.textBox1.Text=ds.Tables[0].DefaultView[0].ToString();
有问题:索引 0 不是为负数,就是大于行数。
应该怎么写????????????????
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
string sql = "select Name from Customer where (CustomerID = 'Hm')";
SqlCommand m_Command = new SqlCommand(sql, conn);
//CommandType m_CommandType = CommandType.Text;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(m_Command);
da.Fill(ds);
this.textBox1.Text=ds.Tables[0].DefaultView[0].ToString();
conn.Close();提示:
this.textBox1.Text=ds.Tables[0].DefaultView[0].ToString();
有问题:索引 0 不是为负数,就是大于行数。
应该怎么写????????????????
this.textBox1.Text=ds.Tables[0].Rows[0][0].ToString(); 这里获得的是结果集里面的第1张表的第一行的第一个字段
--------------------------
DefaultView后面不需要索引。位置0处没有任何行
--------------------
没有记录。从查询分析器验证是否确实有数据。
要不哪个大哥,写个实例出来,看看怎么实现连接sqlserver,然后显示到textbox里面的?
string sql = "select Name from Customer where CustomerID = 'Hm'";
我要崩溃了。原来是hm写错了,hm前面在表里还有个空格,怪不得查其他的行呢。
崩溃啊