热门关键字:   网站安全  黑客攻防  安全漏洞  系统安全  网络安全
站外
广告
域名申请虚拟主机 信息安全 域名注册 云主机 网络安全技术 企业网络安全 站外
广告
文字广告位招租 文字广告位招租 文字广告位招租 文字广告位招租 云安全

ASP.NET mssql server连接数据库

发布时间:2010-11-19 09:49文章来源:网络文章作者:秩名 点击次数:
摘要:// public partial class _default : system.web.ui.page { protected void page_load(object sender, eventargs e) { } protected void btn_sendmessage_click(object sender, eventargs e) { sqlconnection conn = new sqlconnection(@sever = .sqlexpress...

//

public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{

}
protected void btn_sendmessage_click(object sender, eventargs e)
{

sqlconnection conn = new sqlconnection(@"sever = .sqlexpress;database=guestbook; trusted_connection=true");
string sql = "insert into tbguestbook (username,posttime,message,isreplied,reply) values ('test','2006-11-1 12:00','www.hackline.net',0,'ac')";
sqlcommand cmd = new sqlcommand (sql, conn);
conn.open();
cmd.executenonquery();
conn.close();

}

//连接数据库后再查询数据

private void page_load(object sender, system.eventargs e)
{
if(!ispostback)
{
string myconnectstring = "server=localhost;uid=sa;database=manager ";
sqlconnection myconnection =new sqlconnection(myconnectstring);
string comstring = "select * from newstudent ";
sqlcommand mycommand = new sqlcommand(comstring,myconnection);
mycommand.connection=myconnection;
myconnection.open();
dataset ds=new dataset();
sqldataadapter mydataadepter =new sqldataadapter(comstring,myconnection);
mydataadepter.fill(ds, "newstudent ");

datagrid1.datasource=ds.tables[ "newstudent "].defaultview;
datagrid1.databind();
datalist.datasource=ds.tables[ "newstudent "].defaultview;
datalist.databind();
}
}

 

//方法三

在vs中的web.config文件里面可以设置的,
<connectionstrings>
<add name="sqlconnectionstring" connectionstring="data source=pc-200909160824; initial catalog=shopping; integrated security=true"></add>
</connectionstrings>
data source是连接的数据源,initial catalog是你要连接的数据库名,integrated security说明你连接数据库的方式为windows身份验证
%>

 

标签分类:

上一篇:php curl模仿ftp文件上传代码
下一篇:PHP MYSQL 注入攻击的技术实现与预防办法