ASP 怎么连接SQL数据库

ASP 怎么连接SQL数据库,第1张

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 数据表" (将数据表所有记录删除)

using System

using System.Data

using System.Data.SqlClient

using System.Collections

namespace M56kc_Forum

{

/// <summary>

/// DBOperate 的摘要说明。

/// </summary>

public class DBOperate

{

public DBOperate()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

//输入SELECT语句,获得返回数据集

public static DataTable SelectFormForum(string sql,int start,int size,string tablename)

{

try

{

SqlConnection sqlConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"])

SqlDataAdapter sqlAdapter1 = new SqlDataAdapter(sql, sqlConnection)

DataSet returns = new DataSet()

sqlAdapter1.Fill(returns,start,size,tablename)

return returns.Tables[0]

}

catch (Exception ex)

{

throw (ex)

}

}

public static DataTable SelectFormForum(string sql)

{

try

{

SqlConnection sqlConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"])

SqlDataAdapter sqlAdapter1 = new SqlDataAdapter(sql, sqlConnection)

DataSet returns = new DataSet()

sqlAdapter1.Fill(returns)

return returns.Tables[0]

}

catch (Exception ex)

{

throw (ex)

}

}

//返回一条记录

public static DataRow GetDataRow(string sql,string TableName)

{

try

{

SqlConnection sqlConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"])

SqlDataAdapter sqlAdapter1 = new SqlDataAdapter(sql, sqlConnection)

DataSet product = new DataSet()

sqlAdapter1.Fill(product, TableName)

return product.Tables[0].Rows[0]

}

catch (Exception ex)

{

throw (ex)

}

}

//执行SQL语句,如UPDATE,INSERT等

public static void ExcSQL(string s)

{

SqlConnection sqlConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"])

try

{

SqlCommand sqlCmd =new SqlCommand(s,sqlConnection)

sqlConnection.Open()

sqlCmd.ExecuteNonQuery()

}

catch (Exception ex)

{

throw (ex)

}

finally

{

sqlConnection.Close()

}

}

//获取用户短消息条数

public static int ShortMsgCount(string UserName)

{

try

{

string s="SELECT COUNT(*) AS cun FROM ShortMessage WHERE (systemmsg = 0) AND (Readed = 0) AND (UserName = '"+UserName+"')"

DataRow dr=GetDataRow(s,"ShortMessage")

return System.Convert.ToInt32(dr["cun"].ToString())

}

catch

{

return 0

}

}

//计算总共有多少条问题

public static int CalculateQRecord(int BID,int SID,int solved)

{

try

{

string s="SELECT COUNT(*) AS co FROM Questions WHERE (BigClass_ID = "+BID+") AND (SmallClass_ID = "+SID+") AND (kind >0) AND (kind <5) AND (solved = "+solved+")"

DataRow dr=GetDataRow(s,"Questions")

return System.Convert.ToInt32(dr["co"].ToString())

}

catch

{

return 0

}

}

//计算总共有多少条回复

public static int CalculateARecord(int QID)

{

try

{

string s="SELECT COUNT(*) AS co FROM Answers WHERE (Question_ID = "+QID+")"

DataRow dr=GetDataRow(s,"Answers")

return System.Convert.ToInt32(dr["co"].ToString())

}

catch

{

return 0

}

}

}

}

using System  

using System.Collections.Generic  

using System.Linq  

using System.Web  

using System.Web.UI  

using System.Web.UI.WebControls  

using System.Data.SqlClient     //注意需要添加此句  

  

namespace aspnet3  

{  

    public partial class datatest : System.Web.UI.Page  

    {  

        protected void Page_Load(object sender, EventArgs e)  

        {  

            string strconn = "server=localhostuid=sapwd=longltdatabase=School"   

            SqlConnection conn = new SqlConnection(strconn)   //创建连接   

            string sql = "select * from students"   

            conn.Open()   

            SqlCommand cmd = new SqlCommand(sql, conn)      //执行查询  

            Response.Write("连接成功")  

            SqlDataReader dr = cmd.ExecuteReader()          //查询结果  

            if (dr.Read())  

            {  

                 //利用dr[索引]对数据库表进行 *** 作,dr[]返回object;  

                    //可以用字段做索引,也可用列号0,1..做索引  

                Response.Write(dr[0].ToString() + "<br>")  

            }  

  

           // this.Lab.Text = "suc"  

        }  

    }  

}

在上面的例子中,我们连接了一个sa下的School数据库,并查询了其中students字段的内容。

连接数据库分为三个步骤:先定义连接信息,再创建一个连接,最后打开连接

string strconn = "server=localhostuid=sapwd=longltdatabase=School"  //在这一段修改数据库的信息 SqlConnection conn = new SqlConnection(strconn)//创建连接 conn.Open()//打开连接


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

原文地址: http://outofmemory.cn/sjk/10013555.html

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

发表评论

登录后才能评论

评论列表(0条)

保存