1. 添加命名空间System.Data.OracleClient引用
2. using System.Data.OracleClient
3.
string connString = "User ID=IFSAPPPassword=IFSAPPData Source=RACE"
OracleConnection conn = new OracleConnection(connString)
try
{
conn.Open()
MessageBox.Show(conn.State.ToString())
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message.ToString())
}
finally
{
conn.Close()
}
二:通过System.Data.OracleClient(需要安装Oracle客户端不需配置tnsnames.ora)
1. 添加命名空间System.Data.OracleClient引用
2. using System.Data.OracleClient
3.
string connString = "User ID=IFSAPPPassword=IFSAPPData Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = RACE)))"
OracleConnection conn = new OracleConnection(connString)
try
{
conn.Open()
MessageBox.Show(conn.State.ToString())
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message.ToString())
}
finally
{
conn.Close()
}
三:通过System.Data.OleDb和Oracle公司的驱动
1. 添加命名空间System.Data.OracleClient引用
2. using System.Data.OleDb
3.
string connString = "Provider=OraOLEDB.Oracle.1User ID=IFSAPPPassword=IFSAPPData Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = RACE)))"
OleDbConnection conn = new OleDbConnection(connString)
try
{
conn.Open()
MessageBox.Show(conn.State.ToString())
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message.ToString())
}
finally
{
conn.Close()
}
四:通过System.Data.OleDb和微软公司的Oracle驱动
1. 添加命名空间System.Data.OracleClient引用
2. using System.Data.OleDb
3.
string connString = "Provider=MSDAORA.1User ID=IFSAPPPassword=IFSAPPData Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = RACE)))"
OleDbConnection cnn = new OleDbConnection(connString)
try
{
conn.Open()
MessageBox.Show(conn.State.ToString())
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message.ToString())
}
finally
{
conn.Close()
}
如果数据库是SQL 添加引用:using System.Data.SqlClient如果你的数据库是直接建在vs里的话:
string connStr = @"Data Source = .\SQLEXPRESSAttachDBfilename = |DataDirectory|\DBStudent.mdfIntegrated Security = TrueUser Instance = True"
//DBStudent.mdf是我的数据库名字,你改成你的数据库就行了:*.mdf;
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open()
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = ""//你的 *** 作
cmd.ExcuteNonQuery()
}
}
如果数据库是建在你电脑上的数据库里:
string connStr = @"Data Source = .Initial Catalog = DBname(你的数据库名)Integrated Security = True"
其他一样
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)