set conn = Server.CreateObject("Adodb.Connection")
connstr = "Provider=SQLOLEDBdata source=127.0.0.1initial catalog=DBnameuid=usernamepwd=password"
conn.open connstr
set rs = Server.CreateObject("Adodb.RecordSet")
sql = "select id from table"
rs.open sql,conn,1,1
if not rs.eof
response.write "ID为:" &rs("id")
end if
rs.close
conn.close
set conn=nothing
asp使用sql2008连接数据库并添加数据的方法:1、建立数据库连接。
2、使用sql语句插入数据。
举例:
建立数据库连接:
sing System
sing System.Collections.Generic
sing System.Linq
sing System.Web
sing System.Data.SqlClient
// <summary>
// DBHelper 的摘要说明
// </summary>
amespace testDAO.Library
public class DBHelper
{//server=.Trusted_Connection=SSPIdatabase=easylife
private String connectionString = "server=.database=easylifeuid=sapwd=root"
public SqlDataReader ExecuteReader(String sql)
{
SqlConnection connection = new SqlConnection(connectionString)
connection.Open()
SqlCommand command = new SqlCommand(sql,connection)
SqlDataReader result = command.ExecuteReader()
return result
}
public bool ExecuteCommand(String sql)
{
bool result = false
try
{
SqlConnection connection = new SqlConnection(connectionString)
connection.Open()
SqlCommand command = new SqlCommand(sql,connection)
//command.Connection = connection
//command.CommandText = sql
command.ExecuteNonQuery()
connection.Close()
result = true
}
catch (Exception e)
{
throw e
}
return result
}
}
执行插入的方法:
public bool AddUser(User user)
{
bool result = false
String sql = ""
sql = "insert into table_user (userName,userLogin,userPwd)values("
sql += "'" + user.UserName + "',"
sql += "'" + user.UserLogin + "',"
sql += "'" + user.UserPwd + "'"
sql += ")"
DBHelper helper = new DBHelper()
result = helper.ExecuteCommand(sql)
return result
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)