2024年2月20日发(作者:)
data source 和initial catalog
initial catalog与database的区别是什么
Initial Catalog:
DataBase:
两者没有任何区别只是名称不一样,就好像是人类的真实姓名与曾用名一样。。都可以叫你。
********************************************
Integrated Security=SSPI 这个表示以当前WINDOWS系统用户身去登录SQL
SERVER服务器,如果SQL SERVER服务器不支持这种方式登录时,就会出错。
你可以使用SQL SERVER的用户名和密码进行登录,如:
"Provider=SQLOLEDB.1;Persist Security Info=False;Initial Catalog=数据库名;Data Source=192.168.0.1;User ID=sa;Password=密码"
***************************************************
Integrated Security - 或 - Trusted_Connection 'false' 当为 false 时,将在连接中指定用户 ID 和密码。当为 true 时,将使用当前的 Windows 帐户凭据进行身份验证。 可识别的值为 true、false、yes、no 以及与 true 等效的 sspi(强烈推荐)。
*************************************************
中数据库连接方式
nection
常用的一些连接字符串(C#代码):
SqlConnection conn = new SqlConnection( “Server=(local);Integrated
Security=SSPI;database=Pubs“);
SqlConnection conn = new
SqlConnection(“server=(local)NetSDK;database=pubs;Integrated
Security=SSPI“);
SqlConnection conn = new SqlConnection(“Data
Source=localhost;Integrated Security=SSPI;Initial
Catalog=Northwind;“);
SqlConnection conn = new SqlConnection(“ data source=(local);initial
catalog=xr;integrated security=SSPI;
persist security info=False;workstation id=XURUI;packet size=4096; “);
SqlConnection myConn = new
nection(“Persist Security
Info=False;Integrated
Security=SSPI;database=northwind;server=mySQLServer“);
SqlConnection conn = new SqlConnection( “ uid=sa;pwd=passwords;initial
catalog=pubs;data source=127.0.0.1;Connect Timeout=900“);
region "私有变量"
///
/// 表示一个到数据库的打开的连接
///
private nection Con = new
SqlConnection();
///
/// 表示执行对象是SQl还是存储过程
///
private mand Cmd = new
SqlCommand();
///
/// 表示用于填充 t 和更新数据库的一组数据命令和到数据库的连接
///
private aAdapter Dtapt = new
SqlDataAdapter();
///
/// 表示要在数据库中生成的事务
///
private nsaction sqlTran;
///
/// 提供从数据源读取数据行的只进流的方法
///
private SqlDataReader dtrValue = null;
#endregion
#region"数据库连接处理"
///
/// 获得webconfig中的 默认 Sql连接字符串
///
private string strConSql
{
get
{
return
tings["SQLConntionStr"].ToString();
}
}
#endregion
#region "事务处理"
///
/// 开始事务
///
public void BeginTransaction()
{
if ( == )
{
//打开连接
OpenCn();
//开始事务
if (sqlTran == null)
{
sqlTran = ransaction();
}
ction = sqlTran;
}
}
///
/// 提交事务
///
public void CommitTransection()
{
();
e();
sqlTran = null;
CloseCn();
}
///
/// 回滚事务
///
public void RollbackTransection()
{
ck();
e();
sqlTran = null;
CloseCn();
}
#endregion
#region"返回分页表数据Datatable [Read] 方式获取数据,数据量建议在查询结果在10000条记录内"
///
/// 用于分页控件,返回需要显示页的数据和记录条数
///
/// SQL语句
/// SQL参数和其对应值
/// 开始记录
/// 每页显示记录条数
/// 返回记录条数
///
protected DataTable ExecuteReadTable(string p_strSql,
SqlParameter[] p_CmdParms, int p_intStart, int p_intPageSize, ref int
out_intCount)
{
return ExecuteReadTable(, p_strSql, p_CmdParms,
p_intStart, p_intPageSize, ref out_intCount);
}
///
/// 1. 根据存储过程和参数值得到DataTable 值
/// 2. 根据SQL的得到DataTable 值
///
/// 是存储过程还是SQL
/// 开始记录
/// 每页显示条数
/// 可是是SQL 也可以是存储过程
/// SqlParameter参数列表
/// 返回总记录数
///
protected DataTable ExecuteReadTable(CommandType p_objCmdType,
string p_strSql, SqlParameter[] p_CmdParms, int p_intStart, int
p_intPageSize, ref int out_intCount)
{
DataTable dtb = new DataTable();
DateTime dtStart = ;
dtrValue = ExecuteReader(p_objCmdType, p_strSql, p_CmdParms);
if (dtrValue == null)
{
CloseCn();
return dtb;
}
int intColLength = ount;
for (int i = 0; i < intColLength; i++)
{
//构造sql的table
(e(i), GetColType(i));
}
DataRow dr;
int k = 0;
if (s)
{
//读取数据行值
while (())
{
//读取分页间数据
if (p_intStart <= k && k < p_intStart +
p_intPageSize)
{
dr = ();
//读取每列值
for (int j = 0; j < intColLength; j++)
{
//读取每列的值
dr[e(j)] = GetValue(j,
GetFieldType(j).ToString());
}
(dr);
}
k++;
}
//删除了当前页所有数据则读上一页数据
if (k <= p_intStart)
{
while (k <= p_intStart)
{
p_intStart = p_intStart - p_intPageSize;
}
k = 0;
dtrValue = ExecuteReader(p_objCmdType, p_strSql, p_CmdParms);
if (s)
{
while (())
{
//读取分页间数据
if (p_intStart <= k && k < p_intStart +
p_intPageSize)
{
dr = ();
//读取每列值
for (int j = 0; j < intColLength; j++)
{
//读取每列的值
dr[e(j)] =
GetValue(j, GetFieldType(j).ToString());
}
(dr);
}
k++;
}
}
}
}
CloseCn();
og(p_strSql,
ng(), ng(),
ng( - dtStart));
if (out_intCount == 0)
{
out_intCount = k;//获得总行数并且返回到页面
}
return dtb;
}
#endregion
#region "ExecuteReader 执行SQL语句"
///
/// ExecuteReader
///
/// 命令类型 SQL语句
Procedure存储过程
/// 命令类型 1 SQL语句 2存储过程名称
/// SqlParameter
///
private SqlDataReader ExecuteReader(CommandType p_objCmdType,
string p_strSQL, SqlParameter[] p_CmdParms)
{
SqlDataReader dtrRet = null;
try
{
//打开连接
OpenCn();
//命令行连接
tion = Con;
dText = p_strSQL;
//是SQL语句还是存储过程
dType = p_objCmdType;
//循环CmdParms值
if (p_CmdParms != null)
{
foreach (SqlParameter objParm in p_CmdParms)
{
(objParm);
}
}
dtrRet = eReader();
();
return dtrRet;
}
catch (Exception e)
{
string strErr = ; //p_CmdParms 参数值
if (p_CmdParms != null)
{
foreach (SqlParameter objParm in p_CmdParms)
{
strErr += terName + " ='" + + "' ";
}
}
if ( != && sqlTran == null)
{
();
CloseCn();
}
// 写错误日志
or("SqlBase",
e + ""n"r SQL : " + p_strSQL + ""n"r 参数 : " + strErr,
race);
return null;
}
finally
{
//如果连接打开并且没有事务和SqlDataReader事件 则关闭连接
if ( != && sqlTran ==
null && dtrRet == null)
{
();
CloseCn();
}
}
}
#endregion


发布评论