VS2005连接Mysql,SQL Server, Access,Oracle 收藏
1 VS2005连接MySql
下载MySql.Data.dll ,之后添加引用到项目中
添加: using MySql.Data.MySqlClient;
DataSet ds = new DataSet();
string sql = "select * from student";
string constr="server=localhost;User Id=root;password=zhihua1124;Database=mysqldb;";
MySqlConnection con = new MySqlConnection(constr);
con.Open();
try
{
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = new MySqlCommand(sql, con);
adapter.Fill(ds);
con.Close();
}
catch (Exception ex)
{
}
2 VS2005连接SQL Server 2005
string constr="data source=localhost\\sqlexpress;Initial Catalog=sqlserverdb;Integrated Security=True";
string sql="select * from student";
SqlConnection con = new SqlConnection(this.getConnString());
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(sql, con);
adapter.Fill(ds);
3 VS2005连接Access
try
{
string constr= "provider=microsoft.jet.oledb.4.0;"
+@"Data Source=D:\Microsoft Visual Studio 8\Projects\DDBI22\App_Data\accessdb.mdb";
string sql="select * from student";
OleDbConnection con = new OleDbConnection(constr);
con.Open();
OleDbCommand sc = new OleDbCommand(sql, con);
sc.ExecuteNonQuery();
}
catch (SqlException e)
{
Console.Write(e.StackTrace);
}
4 VS2005连接Oracle
添加引用:using System.Data.OracleClient;
try
{
string constr="Data Source=oracledb;user=ddbi;password=ddbi;";
string sql="select * from student";
OracleConnection con = new OracleConnection(constr);
con.Open();
OracleCommand oc = new OracleCommand(sql, con);
oc.ExecuteNonQuery();
}
catch (OracleException e)
{
Console.Write(e.StackTrace);
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。