C# OleDbCommand 和 OleDbDataReader 使用小结

用代码直接说明好了.

using System.Data;

using System.Data.OleDb;

OleDbConnection thisConnection = new OleDbConnection(
      "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("data.mdb"));//获取本地Access

//数据库路径
     thisConnection.Open();

     string strCom = "select * from account where uid = '" + strUid + "' and pwd = '" + strPwd + "'";
     OleDbCommand thisCommand = new OleDbCommand(strCom,thisConnection);
     thisCommand.ExecuteReader();  或者 thisCommand.ExecuteNonQuery(); 

(这两句都可以执行sql语句)

thisConnection.Close();

将下划线部分替换为:

OleDbDataReader thisReader = thisCommand.ExecuteReader();

     if(thisReader.HasRows)

{
      thisReader.Read();
      string strLevel = thisReader["level"].ToString();

}

或者

if(thisReader.Read())

{

string strLevel = thisReader["level"].ToString();

}

OleDbDataReader 中的bool类型HasRows属性可以判断查询结果是否为空.

thisReader["列名"] 可显示查询出的该列数据.

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dfkurama/archive/2005/11/05/523350.aspx

 

你可能感兴趣的:(C++,c,SQL Server,C#,Microsoft)