我得按钮onclick事件如下:
{
string Sqlstring = "server=(local);uid=sa;pwd=123456;database=MyData";
SqlConnection SqlCon = new SqlConnection(Sqlstring);
SqlCon.Open(); string sqlstr = "select UserID,Balance from MYUser ";
SqlCommand SqlCom = new SqlCommand(sqlstr,SqlCon);/*4*/SqlDataReader rdResult = SqlCom.ExecuteReader();
/*3*/this.textBox1.Text = rdResult[0].ToString();
/*2*/rdResult.Close();
/*1*/SqlCon.Close();
}上面/*3*/位置处总说“在没有任何数据时进行无效的读取尝试”,应该是说没有数据的时候我用了ToString方法,也就是说没有读出来数据。而我把1~4行变为,只读取第一行第一列,可以顺利执行显示出我的数据,这证明我上面的数据库联接应该没有问题,不知道用ExecuteReader 有什么注意事项吗?
{
string strTemp = SqlCom.ExecuteScalar();
this.textBox1.Text = strTemp;
SqlCon.Close();
}
{
string Sqlstring = "server=(local);uid=sa;pwd=123456;database=MyData";
SqlConnection SqlCon = new SqlConnection(Sqlstring);
SqlCon.Open(); string sqlstr = "select UserID,Balance from MYUser ";
SqlCommand SqlCom = new SqlCommand(sqlstr,SqlCon);/*4*/SqlDataReader rdResult = SqlCom.ExecuteReader();
/*3*/this.textBox1.Text = rdResult[0].ToString();
/*2*/rdResult.Close();
/*1*/SqlCon.Close();
}上面/*3*/位置处总说“在没有任何数据时进行无效的读取尝试”,应该是说没有数据的时候我用了ToString方法,也就是说没有读出来数据。而我把1~4行变为,只读取第一行第一列,可以顺利执行显示出我的数据,这证明我上面的数据库联接应该没有问题,不知道用ExecuteReader 有什么注意事项吗?
{
string strTemp = SqlCom.ExecuteScalar();
this.textBox1.Text = strTemp;
SqlCon.Close();
}
解决方案 »
- 如何设置打印DataGridView的打印
- 关于WCF宿舍启动多个服务
- 多线程问题,附线程关闭主线程也跟着关闭~~
- 求高手,怎么从txt文件读取出来的数组,在利用数组进行分段。。急求解
- 如何能够使计算器连续运算加减乘除?下面的代码只能生成一个只能运算一次加减乘除的计算器
- 字符串显示问题(十分不解)
- 怎样才能提出来实时的Text值?
- 做电脑几年了,病也做出来了,郁闷中,痛苦中!求治疗的好方法!非常感谢!
- 请问如何读取和设置文件和目录的NTFS权限,100要例子。
- 从打开的子窗口获得返回值?
- 毕业设计c#做的有趣小东西给大家看看
- 在线急等~求高手指教~~急急急急急急急急急!!!!数据转换!!!!急急急急急急急急急急急急急急急急急急急急急急急
{
this.textBox1.Text=rdResult[0].ToString()
}
string Sqlstring = "server=(local);uid=sa;pwd=123456;database=MyData";
SqlConnection SqlCon = new SqlConnection(Sqlstring);
SqlCon.Open(); string sqlstr = "select UserID,Balance from MYUser ";
SqlCommand SqlCom = new SqlCommand(sqlstr,SqlCon); SqlDataReader rdResult;
rdResult= SqlCom .ExecuteReader();
// Always call Read before accessing data.
while (rdResult.Read()) {
this.textBox1.Text = rdResult.GetString(0);
}
// always call Close when done reading.
rdResult.Close();
// Close the connection when done with it.
SqlCon.Close();
}