下面有几种连接数据库的方式和对数据库中数据进行增删改查的例子
//通过congfig配置文件来进行连接
配置文件中的代码:
<configuration>
<connectionStrings>
<add name="A" connectionString="DataSource=127.0.0.1,1433;
InitialCatalog=pubs ;user Id=admin;password=admin;
asynchoronous process=true;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
注释:1433: 端口
pubs: 要连接到的数据库
user Id: 数据库连接时的用户名
password: 数据库连接时的密码
//新增
string strConn=Convert.ToString(ConfigurationManager.ConnectionString[“A”])
public bool StoresIns(string id,string name,string state)
{
SqlConnection con=new SqlConnection(strConn);//创建数据库连接对象
SqlCommand cmd=new SqlCommand();//创建sql命令对象
try
{
//设置指令的指令文本的解析类型
cmd.CommandType=System.Data.CommandType.Text;
//设置指令的文本类容
cmd.CommandText=”insert into [pubs].[dbo].[stores] (id,name,state)
values (@id,@name,@state)”;
cmd.Connection=con;
//给参数赋值
cmd.Parameters.Add(new sqlParameter(“@id”,id));
cmd.Parameters.Add(new sqlParameter(“@name”,name));
cmd.Parameters.Add(new sqlParameter(“@state”,state));
//打开数据库
con.Open();
//执行文本内容
int res=cmd.ExcudeNoQuery();
if(res>0)
{
return true;//执行成功
}
else
{
return false;//执行失败
}
}//try结束
catch(Exception ex)//捕获异常
{
Throw new Exception(ex.Message);
}
finally
{
//判断数据库连接
if(con!null&& con.State==System.Data.ConnectionState.Open)
{
If(cmd!=null)
cmd.Dispose(); //释放sql命令资源
con.Close(); //关闭数据库连接
}
}
}
//删除
public bool StoresDel(string id)
{
SqlConnection con=new SqlConnection(strConn);
SqlCommand cmd=new SqlCommand();
try
{
cmd.CommandType=System.Data.CommandType.Text;
cmd.Text=”delete * from [pubs].[dbo].[stores] where id=@id”;
cmd.Parameters.Add(new Parameter(“@id’,id[i]));
cmd.Connection=con;
con.open();
cmd.ExecuteNoquery();
} //try结束
catch
{
if(ot!=null)//执行错误时回滚事务
ot.Rollback();
return false;
}//catch结束
finally
{
if(con!=null&&con.State=System.Data.ConnectionState.Open())
{
If(cmd!=null)
cmd.Dispose();
con.Close();
}
}//fimally结束
return true;
}
//修改
Public bool StoresUpd(string id,string name,string state)
{
SqlConnection con=new SqlConnection();
SqlCommand cmd=new SqlCommand();
try
{
cmd.ContantType=System.Data.ContantType.Text;
cmd.CommandText=”update [pubs].[dbo].[stores]
set name=@name,state=@state where id=@id”;
cmd.Parameters.Add(new Parameter(@name,name));
cmd.Parameters.Add(new Parameter(@state,state));
cmd.Parameters.Add(new Parameter(@id,id));
cmd.Connection=con;
con.Open();
int result=cmd.ExcuteNoquery();
if(result>0)
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)//捕获异常
{
Throw new Exception(ex.Message);
}
finally
{
//判断数据库连接
if(con!null&& con.State==System.Data.ConnectionState.Open)
{
If(cmd!=null)
cmd.Dispose(); //释放sql命令资源
con.Close(); //关闭数据库连接
}
}
}
//查询
Public void StoresSel()
{
SqlConnection con=new SqlConnection(strCon);
SqlCommand cmd=new SqlCommand();
DataSet ds=new DataSet();
try
{
cmd.CommandType=System.Data.CommandType.Text;
cmd.CommandText=”select * from [pubs].[dbo].[stores]”;
cmd.Connection=con;
SqlAdapter da=new SqlAdapter();
da.SelectCommand=cmd;
con.Open();
da.fill(ds,”stores”);
GridView.DataSource=ds.Tables[“stores”];
GridView.DataBind();
}
cath(Exception ex)
{
Throw new Exception(ex.Message);
}
finally
{
if(con!null&& con.State==System.Data.ConnectionState.Open)
{
If(cmd!=null)
cmd.Dispose(); //释放sql命令资源
con.Close(); //关闭数据库连接
}
da.Dispose();
}
}
通过构造类进行数据库连接
//数据库连接类
public static class Conn
{
public static string constr=”Data Source=E6-65-PC;Intial catlog=pubs;Integrated Security=True”;
//连接采用windows验证时,将连接字符串改为:
//server=(local);integrated security=SSPI;database=pubs
public static SqlConnection con=new SqlConnection(constr);
}
//新增
public bool StoresIns(string id,string name,string state)
{
SqlCommand cmd=new SqlCommand();//创建sql命令对象
try
{
//设置指令的指令文本的解析类型
cmd.CommandType=System.Data.CommandType.Text;
//设置指令的文本类容
cmd.CommandText=”insert into [pubs].[dbo].[stores] (id,name,state)
values (@id,@name,@state)”;
cmd.Connection=Conn.con;
//给参数赋值
cmd.Parameters.Add(new sqlParameter(“@id”,id));
cmd.Parameters.Add(new sqlParameter(“@name”,name));
cmd.Parameters.Add(new sqlParameter(“@state”,state));
//打开数据库
con.Open();
//执行文本内容
int res=cmd.ExcudeNoQuery();
if(res>0)
{
return true;//执行成功
}
else
{
return false;//执行失败
}
}//try结束
catch(Exception ex)//捕获异常
{
Throw new Exception(ex.Message);
}
finally
{
//判断数据库连接
if(con!null&& con.State==System.Data.ConnectionState.Open)
{
If(cmd!=null)
cmd.Dispose(); //释放sql命令资源
con.Close(); //关闭数据库连接
}
}
}
//删除
public bool StoresDel(string id)
{
SqlCommand cmd=new SqlCommand();
try
{
cmd.CommandType=System.Data.CommandType.Text;
cmd.Text=”delete * from [pubs].[dbo].[stores] where id=@id”;
cmd.Parameters.Add(new Parameter(“@id’,id[i]));
cmd.Connection=Conn.con;
con.open();
cmd.ExecuteNoquery();
} //try结束
catch
{
if(ot!=null)//执行错误时回滚事务
ot.Rollback();
return false;
}//catch结束
finally
{
if(con!=null&&con.State=System.Data.ConnectionState.Open())
{
If(cmd!=null)
cmd.Dispose();
con.Close();
}
}//fimally结束
return true;
}
//修改
Public bool StoresUpd(string id,string name,string state)
{
SqlCommand cmd=new SqlCommand();
try
{
cmd.ContantType=System.Data.ContantType.Text;
cmd.CommandText=”update [pubs].[dbo].[stores]
set name=@name,state=@state where id=@id”;
cmd.Parameters.Add(new Parameter(@name,name));
cmd.Parameters.Add(new Parameter(@state,state));
cmd.Parameters.Add(new Parameter(@id,id));
cmd.Connection=Conn.con;
con.Open();
int result=cmd.ExcuteNoquery();
if(result>0)
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)//捕获异常
{
Throw new Exception(ex.Message);
}
finally
{
//判断数据库连接
if(con!null&& con.State==System.Data.ConnectionState.Open)
{
If(cmd!=null)
cmd.Dispose(); //释放sql命令资源
con.Close(); //关闭数据库连接
}
}
}
//查询
Public void StoresSel()
{
SqlCommand cmd=new SqlCommand();
DataSet ds=new DataSet();
try
{
cmd.CommandType=System.Data.CommandType.Text;
cmd.CommandText=”select * from [pubs].[dbo].[stores]”;
cmd.Connection=Conn.con;
SqlAdapter da=new SqlAdapter();
da.SelectCommand=cmd;
con.Open();
da.fill(ds,”stores”);
GridView.DataSource=ds.Tables[“stores”];
GridView.DataBind();
}
cath(Exception ex)
{
Throw new Exception(ex.Message);
}
finally
{
if(con!null&& con.State==System.Data.ConnectionState.Open)
{
If(cmd!=null)
cmd.Dispose(); //释放sql命令资源
con.Close(); //关闭数据库连接
}
da.Dispose();
}
}