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
}
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 数据表" (将数据表所有记录删除)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)