ASP.NET中 *** 作SQL数据库(连接字符串的配置及获取)

ASP.NET中 *** 作SQL数据库(连接字符串的配置及获取),第1张

在WebConfig中配置数据库连接字符串 代码如下

复制代码 代码如下: <connectionStrings><add name=ConnectionString connectionString="user id=用户名password=密码initial catalog=数据库名称data source=服务器名称"/></connectionStrings>

然后在Webform_ aspx cs里面获取连接字符串 要添加如下引用

复制代码 代码如下: using System Configurationusing System Datausing System Data SqlClient 复制代码 代码如下: SqlConnection conprotected void Page_Load(object sender EventArgs e) { ConnectDB()} private void ConnectDB() { string ConString = ConfigurationManager ConnectionStrings["ConnectionString"] ConnectionStringcon = new SqlConnection(ConString)con Open()SqlCommand = new SqlCommand()SqlDataReader sdrstring sqlstr = "select * from item"CommandText = sqlstrConnection = consdr = ExecuteReader()while (sdr Read()) { Response Write(sdr["字段"] ToString()+"</br>")} sdr Close()sdr = null} lishixinzhi/Article/program/net/201311/14106

using syetem.data //引用system.data命名空间,这里封装了一些与数据 *** 作有关的对象,比如DataTable\x0d\x0ausing system.data.sqlclient //引用system.data.sqlclient命名空间,这里封装了很多与SQl Server *** 作相关的类比如下面用到的SqlConnection\x0d\x0a \x0d\x0astring constr=system.configuration.configurationmanager.connectionstrings["connstring"].Tostring()\x0d\x0a\x0d\x0a//获取一个定义的连接字符串,该字符串定义在app.config或者Web.config文件中,其中有一个节的名称为connstring\x0d\x0a\x0d\x0asqlconnection conn =new sqlconnection(connstr) //定义一个SqlConnection对象并实例化,采用了带ConnectiongString的构造函数来创建对象。

ASP与SQL数据库连接语句具体如下:

Set conn = Server.CreateObject("ADODB.Connection")

connstr = "provider=Sqloledbserver=服务器名uid=用户名pwd=密码database=数据库名"

conn.Open connstr

If Err Then

err.Clear

Set conn = Nothing

Response.Write "数据库连接出错,请检查连接字串"

Response.End

扩展资料:

SQL常用命令使用方法:

(1) 数据记录筛选:

sql="select * from 数据表 where 字段名=字段值 order by 字段名 "

sql="select * from 数据表 where 字段名 like ‘%字段值%‘ order by 字段名 "

sql="select top 10 * from 数据表 where 字段名 order by 字段名 "

sql="select * from 数据表 where 字段名 in (‘值1‘,‘值2‘,‘值3‘)"

sql="select * from 数据表 where 字段名 between 值1 and 值2"

(2) 更新数据记录:

sql="update 数据表 set 字段名=字段值 where 条件表达式"

sql="update 数据表 set 字段1=值1,字段2=值2 …… 字段n=值n where 条件表达式"

(3) 删除数据记录:

sql="delete from 数据表 where 条件表达式"

sql="delete from 数据表" (将数据表所有记录删除)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9664976.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存