导航:首页 > 编程大全 > 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

友情链接