c#数据库连接

时间:2023-01-08 13:37:25

c#数据库连接

//此方法是使用Windows身份验证连接本地sql数据库
using
System;using System.Data;
using System.Data.SqlClient;//连接sql数据库,需自己添加
using System.Windows.Forms;
namespace DataSearching
{
public partial class Form1 : Form
{

private string ConnectionString ="Server = .;database=MyDatabase;integrated security = sspi";
private SqlConnection conn = null;
private SqlDataAdapter DataAdapter = null;
private DataSet dataset = null;
private SqlCommand cmd = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string tb1 = textBox1.Text;
if (comboBox2.Text == " like ") tb1 = "%" + textBox1.Text + "%";
string strSQL = "SELECT * FROM student Where ";
strSQL
+= comboBox1.Text + comboBox2.Text + "'" + tb1 + "'";
try
{
cmd.CommandText
= strSQL;
DataAdapter.SelectCommand
= cmd;
dataset.Clear();
DataAdapter.Fill(dataset,
"t1");
dataGridView1.DataSource
= dataset;
dataGridView1.DataMember
= "t1";
}
catch
{
MessageBox.Show(
"请正确设置检索条件!");
}
finally
{
if (conn != null) conn.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
conn
= new SqlConnection(ConnectionString);
conn.Open();
DataAdapter
= new SqlDataAdapter();
dataset
= new DataSet();
cmd
= new SqlCommand();
cmd.Connection
= conn;
cmd.CommandText
= "SELECT * FROM student";
DataAdapter.SelectCommand
= cmd;
DataAdapter.Fill(dataset,
"t1");
comboBox1.Items.Clear();
//先获取所有的字段,以用于构造查询条件
for (int i = 0; i < dataset.Tables["t1"].Columns.Count; i++)
comboBox1.Items.Add(dataset.Tables[
"t1"].Columns[i].ToString());
dataset.Clear();
comboBox2.Items.Add(
" = "); //设置比较运算符
comboBox2.Items.Add(" < ");
comboBox2.Items.Add(
" > ");
comboBox2.Items.Add(
" like ");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

 

要连接Oracle数据库,需先添加OracleClient引用,然后引用System.Data.OracleClient命名空间,在使用OracleConnection对象进行连接。

注意:在使用VS时会找不到System.Data.OracleClient这个引用,可以使用以下方法解决;右击项目的属性,

在弹出窗口中有一个【目标框架】下拉框选项,默认全是,.Net FRAMWORK 4 CLIENT PROFILE(NET Framwork 4 Client profile 是 .NET Framework 4 简精版 ),

将此选项更改为 .NET FRAMEWORK 4。再打开【解决方法资源管理器】窗口,在项目的【引用】上单击鼠标右键,在弹出的快捷菜单中选择【添加引用】命令,添加OracleClient引用,就ok了

using System;
using System.Data;
using System.Data.OracleClient;
using System.Windows.Forms;
namespace DataSearching
{
public partial class Form1 : Form
{

private string ConnectionString = "User Id = sunc; Password = hr;Data Source = xe";
private OracleConnection conn = null;
private OracleDataAdapter DataAdapter = null;
private DataSet dataset = null;
private OracleCommand cmd = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string tb1 = textBox1.Text;
if (comboBox2.Text == " like ") tb1 = "%" + textBox1.Text + "%";
string strSQL = "SELECT * FROM student Where ";
strSQL
+= comboBox1.Text + comboBox2.Text + "'" + tb1 + "'";
try
{
cmd.CommandText
= strSQL;
DataAdapter.SelectCommand
= cmd;
dataset.Clear();
DataAdapter.Fill(dataset,
"t1");
dataGridView1.DataSource
= dataset;
dataGridView1.DataMember
= "t1";
}
catch
{
MessageBox.Show(
"请正确设置检索条件!");
}
finally
{
if (conn != null) conn.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
conn
= new OracleConnection(ConnectionString);
conn.Open();
DataAdapter
= new OracleDataAdapter();
dataset
= new DataSet();
cmd
= new OracleCommand();
cmd.Connection
= conn;
cmd.CommandText
= "SELECT * FROM student";
DataAdapter.SelectCommand
= cmd;
DataAdapter.Fill(dataset,
"t1");
comboBox1.Items.Clear();
//先获取所有的字段,以用于构造查询条件
for (int i = 0; i < dataset.Tables["t1"].Columns.Count; i++)
comboBox1.Items.Add(dataset.Tables[
"t1"].Columns[i].ToString());
dataset.Clear();
comboBox2.Items.Add(
" = "); //设置比较运算符
comboBox2.Items.Add(" < ");
comboBox2.Items.Add(
" > ");
comboBox2.Items.Add(
" like ");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
其中 private string ConnectionString = "User Id = sunc; Password = hr;Data Source = xe";
User Id 为用户名,password 为用户名对应的密码;
Data Sourse 获取方式:打开数据库,连接上用户,右击用户名查看属性 对应SID内容即为DATA SOURCE;