導航:首頁 > 編程大全 > sql資料庫只能連上一個

sql資料庫只能連上一個

發布時間:2024-07-03 03:46:35

⑴ 求助:如何在一個項目中使用多個SQL資料庫

相信你這樣一系統,肯定用了三層架構的模式,這樣的話就好解決了,在數據訪問層專一個連接資料庫的有參方法。在調用該方法時傳遞不同資料庫名即可,當然,每次在資料庫訪問層操作後一定要斷掉數據連接,對一些實例清空回收,這很重要。
public DatabaseClass(String databaseName )
{
strDbConnection="server=.;uid=sa;pwd=sa;database="+ databaseName;
}
給你一點我以前的代碼,或許對你有用。
------------------------------------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.ComponentModel;

namespace Common
{

public class DatabaseClass
{

static String strDbConnection=""; // 保存資料庫連接字元串
static bool isCon; // 資料庫連接標志
static SqlConnection con; //資料庫連接對象
static SqlCommand cmd; //SQL命令對象
private SqlTransaction _sqlTransaction; //事務對象
private bool _isStartTransaction; //事務開啟標志

//無參構造
public DatabaseClass()
{
//strDbConnection="server=.;uid=sa;pwd=sa;database=emporiumDB";
strDbConnection="server=.;uid=sa;pwd=sa;database=BankDB";
}

//有參構造(從外部傳入連接串)
public DatabaseClass(String ConnectionString)
{
if (ConnectionString != "")
{
strDbConnection = ConnectionString;
con=null;
isCon =false;
}
}

//打開資料庫連接,成功返回true
private bool ConnectDb()
{
//若資料庫不處於連接狀態,則執行連庫操作
if (!isCon)
{
try
{
//如果沒有連接,就實例化一個新的連接
if (con == null)
{
con = new SqlConnection(strDbConnection);
con.Open();

}

//有SQL命令有無例化對象,沒就就實例化一新的。
if (cmd == null)
{
cmd = new SqlCommand();
}
isCon = true;
cmd.Connection = con;
}
catch(Exception e)
{
throw e;
}
}
return true;
}

//執行單向SQL語句,如插入、刪除、修改,成功返回true
public bool Execute(string sSql)
{

if (!ConnectDb())
{
throw(new ApplicationException("沒有建立資料庫連接"));
}

cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = sSql;
try
{
cmd.ExecuteNonQuery();
return true;
}
catch(SqlException e)
{
throw new Exception("執行SQL語句時出現問題"+e.Message );

}
catch(Exception e)
{
throw e;

}
finally
{
Dispose();
}

}

//執行返回int型單值的SQL語句
public int ExecuteScalar(string sql)
{
int count=0;

if (!ConnectDb())
{
throw new Exception("沒有建立資料庫連接");
}

cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = sql;
try
{
count=(int)cmd.ExecuteScalar();
return count;
}
catch(SqlException e)
{
throw new Exception("執行SQL語句時出現問題"+e.Message );

}
catch(Exception e)
{
throw e;
}
finally
{
Dispose();
}

}

//執行返回字元串類型單值的SQL語句
public string ExecuteString(string sql)
{
string str="";

if (!ConnectDb())
{
throw new Exception("沒有建立資料庫連接");
}

cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = sql;
try
{
str=(string)cmd.ExecuteScalar();
return str;
}
catch(SqlException e)
{
throw new Exception("執行SQL語句時出現問題"+e.Message );

}
catch(Exception e)
{
throw e;
}
finally
{
Dispose();
}

}

//把tableName表中的數據填充到數據集dataSet中,成功返回true
public bool Select(DataSet dataSet, string tableName, string queryString)
{
//若連接資料庫失敗則產生異常
if (!ConnectDb())
{
throw new Exception("資料庫沒有連接。");
}

try
{
//執行SQL語句,並將執行結果填充到DataSet的表中
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = queryString;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(dataSet,tableName);
return true;
}
catch(SqlException e)
{
throw new Exception("資料庫錯誤。"+e.Message );

}
catch(Exception ex)
{
throw ex;
}
finally
{
Dispose();
}

}

// 查詢資料庫,返回查詢結果表,SQL查詢語句,成功返回查詢結構表,失敗返回null
public DataTable Select(String l_sQuery,String l_sTableName )
{
String sql = l_sQuery;
String sTableName = l_sTableName;

//若連接資料庫失敗則返回空
if (!ConnectDb())
{
throw new Exception ("資料庫連接失敗");;
}

DataSet cDataSet = new DataSet();
try
{
//執行SQL語句,並將執行結果充到DataSet的表中
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = sql;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(cDataSet,sTableName);
}
catch(SqlException e)
{
throw e;

}
finally
{
Dispose();
}
DataTable tempTable = cDataSet.Tables[sTableName];
cDataSet.Tables.Remove(sTableName);//從集合中移除該DataTable
return tempTable;

}

//開啟事務
public void StartTransation()
{
//若連接資料庫失敗拋出錯誤
if (!ConnectDb())
{
throw(new ApplicationException("沒有建立資料庫連接。"));
}

this._isStartTransaction = true;
this._sqlTransaction = con.BeginTransaction(IsolationLevel.ReadCommitted);
cmd.Transaction = _sqlTransaction;

}

// 當前待處理事務提交,失敗全部回滾, 成功提交返回true
public bool Commit()
{
//如果沒有開啟事務處理功能,不做任何操作,直接返回成功
if (!_isStartTransaction)
{
return true;
}
try
{
_sqlTransaction.Commit();
}
catch(SqlException e)
{
_sqlTransaction.Rollback();
throw e;
}
return true;
}

//調用三個參數存儲過程,返回一個表
public DataTable RunProcere(string procName,string pName1,string pValue1,
string pName2,string pValue2,string pName3,string pValue3)
{
//若連接資料庫失敗拋出錯誤
if (!ConnectDb())
{
throw(new ApplicationException("沒有建立資料庫連接。"));
}
try
{
cmd=new SqlCommand (procName,con);
cmd.CommandType =CommandType.StoredProcere ;

//創建輸入參數
SqlParameter sq1=new SqlParameter(pName1,SqlDbType.Int);
sq1.Direction =ParameterDirection.Input ;
sq1.Value =pValue1;
cmd.Parameters .Add (sq1);

SqlParameter sq2=new SqlParameter(pName2,SqlDbType.VarChar);
sq1.Direction =ParameterDirection.Input ;
sq1.Value =pValue2;
cmd.Parameters .Add (sq2);

SqlParameter sq3=new SqlParameter(pName3,SqlDbType.VarChar);
sq1.Direction =ParameterDirection.Input ;
sq1.Value =pValue3;
cmd.Parameters .Add (sq3);

// //創建輸出參數
// SqlParameter spid=new SqlParameter ("@spid",SqlDbType.VarChar,20);
// spid.Direction =ParameterDirection.Output ;
// sqlCmd.Parameters .Add (spid);//
// //創建接收返回值的參數
// SqlParameter sReturn=new SqlParameter ("@RETURN_VALUE",SqlDbType.Int);
// sReturn.Direction =ParameterDirection.ReturnValue ;
// sqlCmd.Parameters .Add (sReturn);

SqlDataAdapter da=new SqlDataAdapter ();
da.SelectCommand=cmd;
DataSet ds1=new DataSet ();
da.Fill (ds1);
return ds1.Tables[0];
}
catch(Exception e)
{
throw e;
}
finally
{
Dispose();
}

}

//調用儲過程(存儲過程名,參數名,參數值,參數類型)
public DataTable RunProcere(string procName,string pName,string pValue,SqlDbType type)
{
//若連接資料庫失敗拋出錯誤
if (!ConnectDb())
{
throw(new ApplicationException("沒有建立資料庫連接。"));
}
try
{
cmd=new SqlCommand (procName,con);
cmd.CommandType =CommandType.StoredProcere ;

//創建輸入參數
SqlParameter sq1=new SqlParameter(pName,type);
sq1.Direction =ParameterDirection.Input ;
sq1.Value =pValue;
cmd.Parameters .Add (sq1);

SqlDataAdapter da=new SqlDataAdapter ();
da.SelectCommand=cmd;
DataSet ds1=new DataSet ();
da.Fill (ds1);
return ds1.Tables[0];
}
catch(Exception e)
{
throw e;
}
finally
{
Dispose();
}

}

// 關閉資料庫,釋放資料庫資源,成功返回true
public bool CloseDb()
{
//釋放資源
Dispose();
return true;
}

//自定義釋放資源,斷掉連接。
public virtual void Dispose(bool disposing)
{
if (! disposing)
return;
//如果資料庫處於連接狀態,則進行斷庫及釋放資源的操作
if (isCon)
{
if (con.State != ConnectionState.Closed )
{
con.Dispose();
con.Close();
cmd = null;
con = null;
_sqlTransaction=null;
isCon = false;
}
}
}

// 除去對象資源.
public void Dispose()
{
//調用帶參數的釋放資源方法
Dispose(true);

GC.SuppressFinalize(true);
}

}
}

⑵ 鍚屼竴IP鍦板潃榪滅▼璁塊棶SQL鏁版嵁搴撳彧鑳戒竴涓鐢ㄦ埛鐧誨綍 鍙︿竴鐢ㄦ埛鐧婚檰錛屽墠涓鐢ㄦ埛灝辨柇寮榪炴帴錛岀浖楂樻墜鎸囩偣錛岃阿璋錛

SQL鏁版嵁搴撲腑鐧婚檰鏃跺欐嬁紼胯韓浠介獙璇佹槸鑷宸變憨鏁忎貢鏈鏈虹殑鐗╃悊鍦板潃錛堜篃縐扮幆鍥炲湴鍧錛夛紝鍜屽叾浠栫殑瀹㈡埛絝娌℃湁鍏崇郴銆傚洜涓轟竴鍙版満瀛愬彧鏈夊敮涓鐨勪竴涓鐗╃悊鍦板潃錛屾墍浠ヤ綘鍦ㄧ櫥榪呮。闄哠QL鐨勬椂鍊欓檺鍒剁櫥闄嗕釜鏁般

⑶ sql如何將一 個資料庫里的某一個表導入另一個資料庫同名的表裡

......表示欄位列表

insertintob.dbo.dept(.....)select...froma.dbo.dept

mysql的話不用寫.dbo

提交回答

閱讀全文

與sql資料庫只能連上一個相關的資料

熱點內容
win101024升級10586 瀏覽:324
spring註解的配置文件 瀏覽:134
已激活win10下載 瀏覽:379
linux查看整個文件內容的命令 瀏覽:309
編程怎麼控制好模縣精度 瀏覽:309
大數據ng 瀏覽:285
電腦故障文件夾路徑 瀏覽:720
word打格式文件樣子 瀏覽:979
蘋果手機怎麼通過備忘錄打開壓縮文件 瀏覽:68
教編程怎麼報名 瀏覽:810
大王卡聯通哪些app免費 瀏覽:267
大數據時代需要學習什麼 瀏覽:690
樂視啟動安卓密碼 瀏覽:758
本地文件如何找到酷狗下載的 瀏覽:898
vs源文件路徑與生成系統路徑 瀏覽:938
計算機網路技術按位與怎麼算 瀏覽:259
lstm代碼 瀏覽:622
耐克網站如何免費抽鞋 瀏覽:229
國外化妝品網站有哪些 瀏覽:772
excel有一個文件一保存就崩潰 瀏覽:382

友情鏈接