using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using System.Collections; namespace DataAccess { public class DBConnect { public static SqlConnection con = null; public static SqlCommand command = null; public static string COMstr = "COM1"; public static string AccessConstr = ""; public static string Connstr = "Data Source=TSS\\TSS_PC;Initial Catalog=LightSystem;Persist Security Info=True;User ID=sa;Password=111"; public static SqlDataReader GetResult(string sql, bool mode, ref string error) { error = ""; SqlDataReader sdr;
try { if(con==null) con = new SqlConnection(Connstr); if (con.State == ConnectionState.Closed) { con.Open();
} if (command == null) { command = new SqlCommand(sql); command.Connection = con; } else command.CommandText = sql;
return new SqlConnection(strCon);
Data Source=.;Initial Catalog=YourDataBaseName;User ID=sa;Password=sa;pooling=true"
这个用的 是windows集成验证登录的,后面的SSPI 指定的是可信任连接!
sever:指定数据库服务器名(local代表本地机器)
database:指定数据库名
<appSettings>
<add key="conn" value="Data Source=LocalHost;Initial Catalog=test;uid=sa;pwd=123"/>
</appSettings>
参考
http://www.cnblogs.com/yongqiudeng/archive/2008/09/05/1067342.html
http://www.cnblogs.com/pincelee/archive/2006/08/01/464672.html
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace DataAccess
{
public class DBConnect
{
public static SqlConnection con = null;
public static SqlCommand command = null;
public static string COMstr = "COM1";
public static string AccessConstr = "";
public static string Connstr = "Data Source=TSS\\TSS_PC;Initial Catalog=LightSystem;Persist Security Info=True;User ID=sa;Password=111";
public static SqlDataReader GetResult(string sql, bool mode, ref string error)
{
error = "";
SqlDataReader sdr;
try
{
if(con==null)
con = new SqlConnection(Connstr);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
if (command == null)
{
command = new SqlCommand(sql);
command.Connection = con;
} else
command.CommandText = sql;
if (!mode)
{
command.ExecuteNonQuery();
return null;
}
else
{
sdr = command.ExecuteReader();
return sdr;
}
}
catch (Exception ex)
{
error = ex.Message;
return null;
}
}
}
自己写的一个简单的SQL访问类,希望能对搂主有用