您的位置:首页 > 数据库

C#访问SQL\Accsee数据库实例

2007-10-04 06:27 330 查看
public DataSet FillDataSet()
{
SqlConnection conn = new SqlConnection("server=(local);Integrated Security=true;Initial Catalog=master;");
SqlCommand cmd = new SqlCommand("SELECT [name], [filename] FROM dbo.sysdatabases", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

try
{
conn.Open();

try
{
da.Fill(ds); // fill DataSet with data
}
catch (Exception)
{
return null;
}
finally
{
conn.Close();
conn.Dispose();
}
}
catch (Exception)
{
return null;
}

return ds;
}
private DataTable FillDataTableByAccess()
{
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + System.IO.Path.GetFullPath("db1.mdb") + "\"";

System.Data.OleDb.OleDbConnection oledbConn = new System.Data.OleDb.OleDbConnection(connString);
System.Data.OleDb.OleDbCommand oledbCom = new System.Data.OleDb.OleDbCommand("SELECT * FROM 客户表",oledbConn);
System.Data.OleDb.OleDbDataAdapter oledbDr = new System.Data.OleDb.OleDbDataAdapter(oledbCom);

DataTable dt = new DataTable();
try
{
oledbConn.Open();

try
{
oledbDr.Fill(dt);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
finally
{
oledbConn.Close();
oledbConn.Dispose();
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}

return dt;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: