<b>ASP.NET mssql server衔接数据库</b>[网站编程]
本文“<b>ASP.NET mssql server衔接数据库</b>[网站编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
//
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身份考证
%>
以上是“<b>ASP.NET mssql server衔接数据库</b>[网站编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |