2.连接sa账户(SQL Server验证登录方式)
string constr = “Data Source =服务器;Initial Catalog=数据库名称;User Id=登录名;Password=密码”;
SQL Server验证:SQLSever2008R2为例,如图所示
实现数据库连接的C#代码如下:
1.方法1:连接数据库,返回DataTable的数据
class Sqlconnn
{
private static string constr = "server=.;Initial Catalog=D_total;Integrated Security=True";//本地数据库
// private static string constr = "Data Source =192.168.1.200;Initial Catalog=D_total ;User Id=sa;Password=123456";//sa账户
public DataTable f1()//应用实例
{
string A = "select * from user";
return Conn(A);
}
//连接数据库,返回DataTable数据--用于查询
public DataTable Conn(string exec)
{
System.Data.DataTable dt_jdl = new DataTable();
try
{
using (SqlConnection con = new SqlConnection(constr))
{
if (con.State == ConnectionState.Closed)
{
con.Open();
} if (con.State == ConnectionState.Open || con.State == ConnectionState.Connecting)
{
SqlDataAdapter sda2 = new SqlDataAdapter(exec, con);//全部通過寫存儲過程即可
DataSet ds2 = new DataSet();
sda2.Fill(ds2, "cxq");
dt_jdl = ds2.Tables["cxq"];
sda2.Dispose();
ds2.Dispose();
}
con.Close();
}
return dt_jdl;
}
catch (Exception EX)
{
return null;
}
}
//通用的增删改
public static int excuteNoQuery(string sql)
{
int count=0;
try
{
using (SqlConnection con = new SqlConnection(constr))
{
if (con.State == ConnectionState.Closed)
{
con.Open();
} if (con.State == ConnectionState.Open || con.State == ConnectionState.Connecting)
{
//创建命令对象
SqlCommand cmd = new SqlCommand(sql, con);
count = cmd.ExecuteNonQuery();
cmd.Dispose();
}
con.Close();
}
return count;
}
catch (Exception EX)
{
return 0;
}
}
}
2.方法2:连接数据库方法
class SqlCon
{
#region 全局变量
//定义一个SqlConnection类型的静态公共变量My_con,用于判断数据库是否连接成功
public static SqlConnection My_con;
//定义数据库连接字符串
public static string Str_sqlcon1 = "server=(local);Initial Catalog=D_total;Integrated Security=True";
public static string Str_sqlcon2 = "Data Source=.;Database=D_total;Userid=sa;PWD=123";
public static string Str_sqlcon3 = "Data Source =192.168.1.200;Initial Catalog=D_total ;User Id=sa;Password=123";
#endregion
///
/// 建立数据库连接
///
///
public static SqlConnection getcon()
{
My_con = new SqlConnection(Str_sqlcon1);//用SqlConnection对象与指定的数据库相连接
My_con.Open();//打开数据库连接
return My_con;//返回SqlConnection对象信息
}
///
/// 关闭数据库连接
///
public void con_close()
{
if (My_con.State == ConnectionState.Open)//判断是否打开与数据库的连接
{
My_con.Close();//关闭数据库的连接
My_con.Dispose();//释放My_con变量的所有空间
}
}
///
/// 以只读方式读取数据库信息
///
/// 表示传递的SQL语句
///
public SqlDataReader getcom(string SQLstr)
{
getcon();//打开数据库连接
SqlCommand My_com = My_con.CreateCommand();//创建SqlConnection对象,用于执行SQL语句
My_com.CommandText = SQLstr;//获取指定的SQL语句
SqlDataReader My_read = My_com.ExecuteReader();//执行SQL语句,生成一个SqlDataReader对象
return My_read;
}
///
/// 通过SqlCommand对象执行数据库中的添加、修改和删除操作
///
/// 表示传递的SQL语句
public void getsqlcom(string SQLstr)
{
getcon();//打开数据库连接
SqlCommand SQLcom = new SqlCommand(SQLstr, My_con);//创建SqlConnection对象,用于执行SQL语句
SQLcom.ExecuteNonQuery();//执行SQL语句
SQLcom.Dispose();//释放所有空间
con_close();//关闭数据库连接
}
///
/// 通过SqlCommand对象执行数据库中的添加、修改和删除操作
///
/// 传递的SQL语句
///
/// DataSet
public DataSet getDataSet(string SQLstr, string tableName)
{
getcon();//打开数据库连接
SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr, My_con);
DataSet ds = new DataSet();//创建DataSet对象
SQLda.Fill(ds, tableName);
con_close();//关闭数据库连接
return ds;//返回DataSet对象信息
}
///
/// 通过SqlCommand对象执行数据库中的添加、修改和删除操作
///
/// 传递的SQL语句
///
/// DataTable
public DataTable getDataTable(string SQLstr, string tableName)
{
DataTable dt = new DataTable();
getcon();//打开数据库连接
SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr, My_con);
DataSet ds = new DataSet();
SQLda.Fill(ds, tableName);
dt = ds.Tables[tableName];
con_close();//关闭数据库连接
return dt;//返回DataSet对象信息
}
}
1.dll下载:https://files.cnblogs.com/yjmyzz/Oracle.ManagedDataAccess.zip
2.可以直接添加引用
3.使用代码:
///
/// oracle数据库基础类
///
public class OracleHepler
{
private static string connStr = "User Id=ae_re;Password=123;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.3.12.202)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=PACKSTB)))";
#region 执行SQL语句,返回受影响行数
public static int ExecuteNonQuery(string sql, params OracleParameter[] parameters)
{
using (OracleConnection conn = new OracleConnection(connStr))
{
conn.Open();
using (OracleCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteNonQuery();
}
}
}
#endregion
#region 执行SQL语句,返回DataTable;只用来执行查询结果比较少的情况
public static DataTable ExecuteDataTable(string sql, params OracleParameter[] parameters)
{
using (OracleConnection conn = new OracleConnection(connStr))
{
conn.Open();
using (OracleCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
OracleDataAdapter adapter = new OracleDataAdapter(cmd);
DataTable datatable = new DataTable();
adapter.Fill(datatable);
return datatable;
}
}
}
#endregion
}
private void button1_Click(object sender, EventArgs e)//winform程式:里面一个button和一个dataGridView
{
string sqlString = "select * from (select * from scqhp.cell_sort_info_by_customer) where ROWNUM<=10 ORDER BY ROWNUM asc";
DataTable dt = OracleHepler.ExecuteDataTable(sqlString, new OracleParameter());
dataGridView1.DataSource = dt;
}