打开指定的access数据库
打开指定的access数据库
/**//// <summary>
/// 打开指定的access数据库
/// </summary>
/// <param name=”spath”>access数据库名</param>
/// <param name=”dataname”>access数据库中的表名</param>
public bool readdata(string spath, string dataname)
{
try
{
//创建一个 OleDbConnection对象
string strCon = “ Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = “ + spath;
OleDbConnection myConn = new OleDbConnection(strCon);
// string strCom = ” SELECT * FROM ” + dataname + ” ORDER BY id”;
//string strCom = ” SELECT * FROM ” + dataname ;
string strCom = “SELECT [“ + dataname + “].* FROM [“ + dataname + “]“;
//创建一个 DataSet对象
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
myCommand.Fill(this.dataSet1, dataname);
myConn.Close();
return true;
}
catch (Exception e)
{
MessageBox.Show(“连接数据库发生错误:“ + e.ToString(), “错误!“, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
/// 打开指定的access数据库
/// </summary>
/// <param name=”spath”>access数据库名</param>
/// <param name=”dataname”>access数据库中的表名</param>
public bool readdata(string spath, string dataname)
{
try
{
//创建一个 OleDbConnection对象
string strCon = “ Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = “ + spath;
OleDbConnection myConn = new OleDbConnection(strCon);
// string strCom = ” SELECT * FROM ” + dataname + ” ORDER BY id”;
//string strCom = ” SELECT * FROM ” + dataname ;
string strCom = “SELECT [“ + dataname + “].* FROM [“ + dataname + “]“;
//创建一个 DataSet对象
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
myCommand.Fill(this.dataSet1, dataname);
myConn.Close();
return true;
}
catch (Exception e)
{
MessageBox.Show(“连接数据库发生错误:“ + e.ToString(), “错误!“, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
版权声明:本文为dreign原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。