服务器名:(计算机名)\sqlexpress
身份验证:Window身份验证
数据库为:Test
如何写连接字符串: Server=.\SQLExpressAttachDbFilename=c:\asd\qwe\mydbfile.mdfDatabase=dbnameTrusted_Connection=Yes
这么写就可以了!!
//定义数据连接private
static
readonly
string
connstr
=
ConfigurationManager.ConnectionStrings["DataBaseString"].ConnectionString
//
ExecQuerySql函数,
执行任意查询sql语句
结果以DataTable形式返回
///
<summary>
///
任意查询sql语句
///
</summary>
///
<param
name="sql">
sql语句</param>
///
<returns>DataTable</returns>
public
static
DataTable
ExecQuerySql(string
sql)
{
try
{
SqlConnection
cn
=
new
SqlConnection(connstr)
SqlCommand
cmd
=
new
SqlCommand(sql,
cn)
cmd.CommandTimeout
=
100000000
SqlDataAdapter
adpt
=
new
SqlDataAdapter(cmd)
DataTable
tbl
=
new
DataTable()
adpt.Fill(tbl)
return
tbl
}
catch
(Exception
ex)
{
throw
new
Exception(ex.Message)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)