展开全部
楼一e69da5e6ba9062616964757a686964616f31333238653964给的个只能连接sql,楼二是忽悠,我的可以连接access,sql,oracle,excel ,纯粹是自用的,
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
namespace CheckMaterial
{
static class SqlHelp
{
///
/// link access
///
public static string connstring=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=cost.mdb;Jet OLEDB:Database Password=1234567";
///
/// link sql
///
public static string connstring = @"Provider=SQLOLEDB; data source=server1;user id=sa;password=;initial catalog=ytdb"
///
/// link oracle
///
//public static string connstring = @"Provider=OraOLEDB.Oracle;Persist Security Info=false;User ID=;Password=;Data Source=";
//public static string connstring = @"Provider=msdaora.1;Persist Security Info=false;User ID=;Password=;Data Source=";
///
/// link excel
///
//public static string connstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelPath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"
///
/// SqlExecuteNonQuery
///
///
/// using try catch to catch the error msg
public static int SqlExecuteNonQuery(string sql)
{
//OleDbConnection conn = new OleDbConnection(connstring);
using (OleDbConnection conn = new OleDbConnection(connstring))
{
try
{
conn.Close();
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
int i = cmd.ExecuteNonQuery();
conn.Close();
return i;
}
catch (Exception e)
{
conn.Close();
//System.Windows.Forms.MessageBox.Show(eee.Message);
throw new Exception(e.Message);
}
}
}
///
/// SqlExecuteNonQuery
///
///
///
/// using try catch to catch the error msg
public static int SqlExecuteNonQuery(string sql, string _connstring)
{
//OleDbConnection conn = new OleDbConnection(connstring);
using (OleDbConnection conn = new OleDbConnection(_connstring))
{
try
{
conn.Close();
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
int i = cmd.ExecuteNonQuery();
conn.Close();
return i;
}
catch (Exception e)
{
conn.Close();
//System.Windows.Forms.MessageBox.Show(eee.Message);
throw new Exception(e.Message);
}
}
}
///
/// SqlExcuteScalar
///
///
/// using try catch to catch the error msg
public static object SqlExcuteScalar(string sql)
{
using (OleDbConnection conn = new OleDbConnection(connstring))
{
try
{
conn.Close();
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
object obj = cmd.ExecuteScalar();
conn.Close();
return obj;
}
catch (Exception e)
{
conn.Close();
throw new Exception(e.Message);
}
}
}
///
/// SqlExcuteScalar
///
///
///
/// using try catch to catch the error msg
public static object SqlExcuteScalar(string sql, string _connstring)
{
using (OleDbConnection conn = new OleDbConnection(_connstring))
{
try
{
conn.Close();
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
object obj = cmd.ExecuteScalar();
conn.Close();
return obj;
}
catch (Exception e)
{
conn.Close();
throw new Exception(e.Message);
}
}
}
///
/// getDataTableFormSql
///
///
/// using try catch to catch the error msg
public static DataTable getDataTableFormSql(string sql)
{
DataTable dt = new DataTable();
using (OleDbConnection conn = new OleDbConnection(connstring))
{
try
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
oda.Fill(dt);
cmd.Dispose();
return dt;
}
catch (Exception e)
{
conn.Close();
throw new Exception(e.Message);
}
}
}
///
/// getDataTableFormSql
///
///
///
/// using try catch to catch the error msg
public static DataTable getDataTableFormSql(string sql, string _connstring)
{
DataTable dt = new DataTable();
using (OleDbConnection conn = new OleDbConnection(_connstring))
{
try
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
oda.Fill(dt);
cmd.Dispose();
return dt;
}
catch (Exception e)
{
conn.Close();
throw new Exception(e.Message);
}
}
}
///
/// SQLExecuteTransaction
///
///
/// using try catch to catch the error msg
public static bool SQLExecuteTransaction(string[] sqls)
{
using (OleDbConnection conn = new OleDbConnection(connstring))
{
try
{
conn.Open();
OleDbTransaction trans = conn.BeginTransaction();
try
{
OleDbCommand cmd = conn.CreateCommand();
cmd.Transaction = trans;
//cmd.CommandText
foreach (string s in sqls)
{
cmd.CommandText = s;
cmd.ExecuteNonQuery();
}
trans.Commit();
return true;
}
catch (Exception e)
{
trans.Rollback();
throw new Exception(e.Message);
}
finally
{
conn.Close();
}
}
catch (Exception e)
{
//trans.Rollback();
throw new Exception(e.Message);
}
}
}
///
/// SQLExecuteTransaction
///
///
///
/// using try catch to catch the error msg
public static bool SQLExecuteTransaction(string[] sqls, string _connstring)
{
using (OleDbConnection conn = new OleDbConnection(_connstring))
{
try
{
conn.Open();
OleDbTransaction trans = conn.BeginTransaction();
try
{
OleDbCommand cmd = conn.CreateCommand();
cmd.Transaction = trans;
//cmd.CommandText
foreach (string s in sqls)
{
cmd.CommandText = s;
cmd.ExecuteNonQuery();
}
trans.Commit();
return true;
}
catch (Exception e)
{
trans.Rollback();
throw new Exception(e.Message);
}
finally
{
conn.Close();
}
}
catch (Exception e)
{
//trans.Rollback();
throw new Exception(e.Message);
}
}
}
}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起