不能这么用ExecuteScalar[C#]
public void ReadMyData(string myConnString) {
string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
myConnection.Open();
SqlDataReader myReader;
myReader = myCommand.ExecuteReader();
// Always call Read before accessing data.
while (myReader.Read()) {
Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
}
// always call Close when done reading.
myReader.Close();
// Close the connection when done with it.
myConnection.Close();
}
public void ReadMyData(string myConnString) {
string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
myConnection.Open();
SqlDataReader myReader;
myReader = myCommand.ExecuteReader();
// Always call Read before accessing data.
while (myReader.Read()) {
Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
}
// always call Close when done reading.
myReader.Close();
// Close the connection when done with it.
myConnection.Close();
}
解决方案 »
- listview代替textbox显示大量字符该怎么操作?
- imagelist图片失真
- 请问string与String的区别
- 反射 动态实例化 如何实例化嵌套类成员?
- 多线程
- 帮忙解释一下,他为什么这么写,关于Enterprise Library中Caching Application Block中的部分!!
- 版主,诸位大侠救急!!编辑windatagrid时,在第一列cell敲回车,根据键入的数据填写同行其他列的内容?
- 我的程序引用Excel,因为退出的时候我要提醒用户是否保存,现在如何判断这个Excel文件的内容已经修改?。
- 下面的语句哪种效率高
- 软件登录的程序应该怎么做?
- 一个简单问题:怎么让一个页面在没人操作10分钟以后跳转到另外一个页面?
- VB做的Socket程序能和C#做和Socket程序通信吗?
执行查询,并返回查询所返回的结果集中第一行的第一列。忽略额外的列或行。
SqlDataReader dr = MyCommand.ExecuteReader();
if(dr.Read())
{
string piaos = dr[0].ToString();
string xingm = dr[1].ToString();
}
sqlConnection MyConnection;
SqlCommand MyCommand;
String ConnStr;
//链接SQL Server数据库
MyConnection =new SqlConnection("Initial Catalog=mydb;Data Source=xxzxw;user id=sa;password=123456;");
MyConnection.Open();
ConnStr="selece piaos,xingm from toup where xingm=zhangxd ";
MyCommand=new SqlCommand(ConnStr,MyConnection);
//数据适配器
SqlDataAdapter da = new SqlDataAdapter();
//连接命令对象
da.SelectCommand = cmd;
//数据集
DataSet ds = new DataSet();//数据集
DataSet ds = new DataSet();