如何把sql server与vs2008连接
1个回答
2015-07-19
展开全部
我的数据库操作类太长,这里贴不下,取几段,希望你看的懂。
public static System.Data.SqlClient.SqlConnection GetConnection()
{
string connstr = ConfigurationManager.ConnectionStrings["SQLString"].ConnectionString;
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connstr);
return conn;
}
public static System.Data.SqlClient.SqlConnection GetConnection(string connStringName)
{
string connstr = null;
if (string.IsNullOrEmpty(connStringName))
{
connstr = ConfigurationManager.ConnectionStrings["SQLString"].ConnectionString;
}
else
{
connstr = ConfigurationManager.ConnectionStrings[connStringName].ConnectionString;
}
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connstr);
return conn;
}
public static object ExecuteScalar(string connStringName, string cmdText, params SqlParameter[] cmdParms)
{
System.Data.SqlClient.SqlConnection conn = GetConnection(connStringName);
System.Data.SqlClient.SqlCommand cmd = new SqlCommand(cmdText, conn);
object obj = null;
try
{
conn.Open();
if (cmdParms != null)
{
foreach (SqlParameter param in cmdParms)
{
cmd.Parameters.Add(param);
}
}
obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
}
finally
{
conn.Close();
}
return obj;
}
public static System.Data.SqlClient.SqlDataReader ExecuteReader(string connStringName, string cmdText, params SqlParameter[] cmdParms)
{
System.Data.SqlClient.SqlConnection conn = GetConnection(connStringName);
System.Data.SqlClient.SqlCommand cmd = new SqlCommand(cmdText, conn);
System.Data.SqlClient.SqlDataReader dr;
try
{
conn.Open();
if (cmdParms != null)
{
foreach (SqlParameter param in cmdParms)
{
cmd.Parameters.Add(param);
}
}
dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return dr;
}
catch (Exception ex)
{
conn.Close();
throw ex;
}
}// ExecuteReader
public static System.Data.SqlClient.SqlConnection GetConnection()
{
string connstr = ConfigurationManager.ConnectionStrings["SQLString"].ConnectionString;
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connstr);
return conn;
}
public static System.Data.SqlClient.SqlConnection GetConnection(string connStringName)
{
string connstr = null;
if (string.IsNullOrEmpty(connStringName))
{
connstr = ConfigurationManager.ConnectionStrings["SQLString"].ConnectionString;
}
else
{
connstr = ConfigurationManager.ConnectionStrings[connStringName].ConnectionString;
}
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connstr);
return conn;
}
public static object ExecuteScalar(string connStringName, string cmdText, params SqlParameter[] cmdParms)
{
System.Data.SqlClient.SqlConnection conn = GetConnection(connStringName);
System.Data.SqlClient.SqlCommand cmd = new SqlCommand(cmdText, conn);
object obj = null;
try
{
conn.Open();
if (cmdParms != null)
{
foreach (SqlParameter param in cmdParms)
{
cmd.Parameters.Add(param);
}
}
obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
}
finally
{
conn.Close();
}
return obj;
}
public static System.Data.SqlClient.SqlDataReader ExecuteReader(string connStringName, string cmdText, params SqlParameter[] cmdParms)
{
System.Data.SqlClient.SqlConnection conn = GetConnection(connStringName);
System.Data.SqlClient.SqlCommand cmd = new SqlCommand(cmdText, conn);
System.Data.SqlClient.SqlDataReader dr;
try
{
conn.Open();
if (cmdParms != null)
{
foreach (SqlParameter param in cmdParms)
{
cmd.Parameters.Add(param);
}
}
dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return dr;
}
catch (Exception ex)
{
conn.Close();
throw ex;
}
}// ExecuteReader
大雅新科技有限公司
2024-11-19 广告
2024-11-19 广告
这方面更多更全面的信息其实可以找下大雅新。深圳市大雅新科技有限公司从事KVM延长器,DVI延长器,USB延长器,键盘鼠标延长器,双绞线视频传输器,VGA视频双绞线传输器,VGA延长器,VGA视频延长器,DVI KVM 切换器等,优质供应商,...
点击进入详情页
本回答由大雅新科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询