<connectionStrings>
<add name="ConStr" connectionString="Data Source=数据库地址Initial Catalog=数据库名User ID=数据库登录名Password=数据库登录密码pooling=TrueMax Pool Size=1000Min Pool Size=5 " providerName="System.Data.SqlClient" />
</connectionStrings>
另可以创建一个数据库访问类
using System
using System.Web
using System.Configuration
using System.Data
using System.Data.SqlClient
using System.Reflection
/// <summary>
/// App_Conn 的摘要说明
/// </summary>
public class App_Conn
{
/// <summary>
/// 得到数据库的地址
/// </summary>
public static string ConnStr
{
get
{
return ConfigurationManager.ConnectionStrings["ConStr"].ToString().Trim()
}
}
public App_Conn()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 返回一个DataSet数据集
/// </summary>
/// <param name="MySQL"></param>
/// <returns></returns>
public static DataSet _DataSet(string MySQL)
{
SqlConnection m_con = new SqlConnection(ConnStr)
if (m_con.State != ConnectionState.Open)
{
m_con.Open()
}
SqlCommand cmd = new SqlCommand(MySQL, m_con)
SqlDataAdapter da = new SqlDataAdapter(cmd)
DataSet ds = new DataSet()
da.Fill(ds)
if (m_con.State == ConnectionState.Open)
{
m_con.Close()
m_con.Dispose()
}
return ds
}
/// <summary>
/// 执行SQL语句后,返回执行的行数
/// </summary>
/// <param name="MySQL"></param>
/// <returns></returns>
public static int _RowCount(string MySQL)
{
SqlConnection m_con = new SqlConnection(ConnStr)
if (m_con.State != ConnectionState.Open)
{
m_con.Open()
}
SqlCommand cmd = new SqlCommand(MySQL, m_con)
int rowscount = cmd.ExecuteNonQuery()
if (m_con.State == ConnectionState.Open)
{
m_con.Close()
m_con.Dispose()
}
return rowscount
}
}
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条)