asp 来连接 sql2008的并添加数据怎么做 100
1个回答
2016-03-14 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
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=SSPI;database=easylife
private String connectionString = "server=.;database=easylife;uid=sa;pwd=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;
}
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=SSPI;database=easylife
private String connectionString = "server=.;database=easylife;uid=sa;pwd=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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询