如何把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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询