C#-与ORACLE直接连接代码.doc
上传者:文库旗舰店
2022-07-11 21:32:58上传
DOC文件
38 KB
C# 与ORACLE直接连接代码.txt生活是过出来的,不是想出来的。放得下的是曾经,放不下的是记忆。无论我在哪里,我离你都只有一转身的距离。第一种:使用connect 与 command ,及 datareader 连接数据库。
private void Form1_Load(object sender, EventArgs e)
{
string connectionString="Data Source=winner;User ID=system;Password=orcl;Unicode=True";
OracleConnection oc = new OracleConnection(connectionString);
oc.Open(); //写在具体的方法里面,上面两句,写在类里面。
string comsel = "select * from product where product_types_id=1";
OracleCommand ocom = new OracleCommand(comsel,oc);
OracleDataReader reader = ocom.ExecuteReader();
string a="";
if (reader.HasRows)
{
if (reader.Read())
{
a=(string)reader["name"]; //oracledatareader reader[""]读取列值,如果为0,说明读取的是第一列;
}
}
this.textBox1.Text = a;
reader.Close();
oc.Close();
}
注:小知识: 内部变量一定要赋值!比如定义在 类 内部的变量 如string a=""; 不能写成string a; 因为a 为内部变量,否则会
报错。
第二种:使用connect,command,及 dataset和dataadapter来连接数据库。
string connectionString="Data Source=winner;User ID=system;Password=orcl;Unicode=True";
OracleConnection oc = new OracleConnection(connectionString);
oc.Open();
private void Form1_Load(object sender, EventArgs e)
{
string connectionString="Data Source=winner;User ID=system;Password=orcl;Unicode=True";
OracleConnection oc = new OracleConnection(connectionString);
oc.Open(); //写在具体的方法里面,上面两句,写在类里面。
string comsel = "select * from product where product_types_id=1";
OracleCommand ocom = new OracleCommand(comsel,oc);
OracleDataReader reader = ocom.ExecuteReader();
string a="";
if (reader.HasRows)
{
if (reader.Read())
{
a=(string)reader["name"]; //oracledatareader reader[""]读取列值,如果为0,说明读取的是第一列;
}
}
this.textBox1.Text = a;
reader.Close();
oc.Close();
}
注:小知识: 内部变量一定要赋值!比如定义在 类 内部的变量 如string a=""; 不能写成string a; 因为a 为内部变量,否则会
报错。
第二种:使用connect,command,及 dataset和dataadapter来连接数据库。
string connectionString="Data Source=winner;User ID=system;Password=orcl;Unicode=True";
OracleConnection oc = new OracleConnection(connectionString);
oc.Open();
C#-与ORACLE直接连接代码