C#連接數(shù)據(jù)庫的幾種方法
更新時間:2022年04月23日 10:41:39 作者:農(nóng)碼一生
這篇文章介紹了C#連接數(shù)據(jù)庫的幾種方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
一、Oracle
查詢
public static DataTable QueryData()
{
DataTable dtResult = new DataTable();
try
{
using (OracleConnection oc = new OracleConnection(HttpContext.Current.Session["DBName"].ToString().Trim()))
{
oc.Open();
string sql = @" SELECT * FROM DUAL";
OracleDataAdapter oaCmd = new OracleDataAdapter(sql, oc);
//oaCmd.SelectCommand.Parameters.Add("fDate", OracleType.VarChar, 50).Value = DateTime.Now.ToString("yyyy/MM/dd") + " " + "00:00:00";
//oaCmd.SelectCommand.Parameters.Add("eDate", OracleType.VarChar, 50).Value = DateTime.Now.ToString("yyyy/MM/dd") + " " + "23:59:59";
oaCmd.Fill(dtResult);
oc.Close();
}
}
catch (Exception ex)
{
}
return dtResult;
}更新
public static void DoInser(string login_user, string login_db)
{
try
{
//string strDBXMLFile = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath.ToString()) + @"\DB.XML";
//DataSet dsXML = new DataSet();
//dsXML.ReadXml(strDBXMLFile);
//DataTable dtAEPDB = dsXML.Tables["DB_NAME"];
//DB 鏈接
string s = "Data Source=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ip地址 )(PORT =端口)))(CONNECT_DATA = (SID=SID號)(SERVER = DEDICATED)));uid = 用戶名; password=密碼;Connection Lifetime=60;Max Pool Size=50;Min Pool Size=0;Pooling=true";
using (OracleConnection oc = new OracleConnection(s))
{
oc.Open();
string sql = @" INSERT INTO 表名 (欄位1, 欄位2, 欄位3,....) VALUES (欄位1值, 欄位2值, 欄位3值, ....)";
OracleCommand oaCmd = new OracleCommand(sql, oc);
//oaCmd.Parameters.Add("參數(shù)", OracleType.VarChar, 30).Value = "";
oaCmd.CommandType = CommandType.Text;
oaCmd.ExecuteNonQuery();
oc.Close();
}
}
catch (Exception ex)
{
}
}二、SQLServer
查詢
public static DataTable QueryData()
{
DataTable dtResult = new DataTable();
try
{
using (SqlConnection sqlConnection = new SqlConnection(HttpContext.Current.Session["DBName"].ToString().Trim()))
{
sqlConnection.Open();
string sql = @" SELECT * FROM DUAL";
SqlDataAdapter sqlDa = new SqlDataAdapter(sql, sqlConnection);
sqlDa.SelectCommand.Parameters.Add("fDate", SqlDbType.VarChar, 50).Value = DateTime.Now.ToString("yyyy/MM/dd") + " " + "00:00:00";
sqlDa.SelectCommand.Parameters.Add("eDate", SqlDbType.VarChar, 50).Value = DateTime.Now.ToString("yyyy/MM/dd") + " " + "23:59:59";
sqlDa.Fill(dtResult);
sqlConnection.Close();
}
}
catch (Exception ex)
{
}
return dtResult;
}更新
public static void DoInser(string login_user, string login_db)
{
try
{
string s = "數(shù)據(jù)庫鏈接";
using (SqlConnection sqlConnection = new SqlConnection(s))
{
sqlConnection.Open();
string sql = @" INSERT INTO 表名 (欄位1, 欄位2, 欄位3,....) VALUES (欄位1值, 欄位2值, 欄位3值, ....)";
SqlDataAdapter sqlDa = new SqlDataAdapter(sql, sqlConnection);
sqlDa.InsertCommand.Parameters.Add("參數(shù)", SqlDbType.VarChar, 30).Value = "";
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
sqlConnection.Close();
}
}
catch (Exception ex)
{
}
}工具類
public DataTable ExecuteQuery(string sqlStr) //用于查詢;其實是相當(dāng)于提供一個可以傳參的函數(shù),到時候?qū)懸粋€sql語句,存在string里,傳給這個函數(shù),就會自動執(zhí)行。
{
SqlConnection con = new SqlConnection("MySqlCon");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlStr;
DataTable dt = new DataTable();
SqlDataAdapter msda;
msda = new SqlDataAdapter(cmd);
msda.Fill(dt);
con.Close();
return dt;
}
public int ExecuteUpdate(string sqlStr) //用于增刪改;
{
SqlConnection con = new SqlConnection("MySqlCon");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlStr;
int iud = 0;
iud = cmd.ExecuteNonQuery();
con.Close();
return iud;
}到此這篇關(guān)于C#連接數(shù)據(jù)庫的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- C#?連接本地數(shù)據(jù)庫的實現(xiàn)示例
- 使用C#連接并讀取MongoDB數(shù)據(jù)庫
- C#連接SQL數(shù)據(jù)庫和查詢數(shù)據(jù)功能的操作技巧
- C#如何連接MySQL數(shù)據(jù)庫
- C#連接SQL Server數(shù)據(jù)庫的實例講解
- C#連接Oracle數(shù)據(jù)庫字符串(引入DLL)的方式
- C#連接加密的Sqlite數(shù)據(jù)庫的方法
- C#簡單訪問SQLite數(shù)據(jù)庫的方法(安裝,連接,查詢等)
- C#簡單連接sql數(shù)據(jù)庫的方法
- C#程序連接數(shù)據(jù)庫及讀取數(shù)據(jù)庫中字段的簡單方法總結(jié)
相關(guān)文章
c#判斷字符是否為中文的三種方法分享(正則表達(dá)式判斷)
判斷一個字符是不是漢字通常有三種方法,第一種用 ASCII 碼判斷,第二種用漢字的UNICODE編碼范圍判斷,第三種用正則表達(dá)式判斷,以下是具體方法2014-01-01
c# 實現(xiàn)子窗口關(guān)閉父窗口也關(guān)閉的簡單實例
下面小編就為大家?guī)硪黄猚# 實現(xiàn)子窗口關(guān)閉父窗口也關(guān)閉的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
DevExpress實現(xiàn)GridControl列頭繪制Checkbox的方法
這篇文章主要介紹了DevExpress實現(xiàn)GridControl列頭繪制Checkbox的方法,需要的朋友可以參考下2014-08-08

