ASP.NET中操作SQL数据库
在webconfig中配置连接字符串,代码如下:
<connectionstrings>
<add name="connectionstring" connectionstring="user id=用户名;password=密码;initial catalog=数据库名称;data source=服务器名称"/>
</connectionstrings>
然后在webform_1.x.cs里面获取连接字符串,要添加如下引用;
using system.configuration;
using system.data;
using system.data.sqlclient;
代码:
sqlconnection con;
protected void page_load(object sender, eventargs e)
{
connectdb();
}
private void connectdb()
{
string constring = configurationmanager.connectionstrings["connectionstring"].connectionstring;
con = new sqlconnection(constring);
con.open();
sqlcommand com = new sqlcommand();
sqldatareader sdr;
string sqlstr = "select * from item";
com.commandtext = sqlstr;
com.connection = con;
sdr = com.executereader();
while (sdr.read())
{
response.write(sdr["字段名"].tostring()+"</br>");
}
sdr.close();
sdr = null;
}
上一篇: CSS3打造百度贴吧的3D翻牌效果示例
下一篇: 学问和诗歌