求一个用Web.config连接数据库的实例 用add key 或者 add name那种连接方法比如我有个数据库:168 表:users 字段:name 字段:word 在index.aspx里 用datareader读取 并输出这两个字段希望有:Web.config连接库的部分 和index.aspx 的方法部分 代码 谢谢啦 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是不是你要的 <add key="strProvider" value="data source=px14;initial catalog=gxy_new;persist security info=False;user id =sa;workstation id=YJZ;packet size=4096;pwd=123456;"/> 是 但是 还需要 读取的实例部分 数据库:168 表:users 字段:name 字段:word 在index.aspx里 用datareader读取 并输出这两个字段 <add name="DBConnect" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\168.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" /><asp:SqlDataSource ID="SDS" runat="server" ConnectionString="<%$ ConnectionStrings:Database23ConnectionString1 %>" SelectCommand="SELECT [name] ,[word] FROM [user] "></asp:SqlDataSource> <appSettings><add key="HumanResources" value="server=(localhost);Connection Timeout=30;database=HumanTables;uid=;pwd="/> </appSettings> key 和 name 的区别是 什么? <appSettings> <add key="FJPConnectionString" value="Data Source=192.168.1.1;Initial Catalog=数据库;Persist Security Info=True;User ID=sa;Password=密码"></add> </appSettings>public bool funOpenCn() { try { SqlConnection adoCn = (SqlConnection)HttpContext.Current.Session["new clsDataBase().funGetConntion()"]; if (adoCn != null) { if (adoCn.State == ConnectionState.Open) { adoCn.Close(); } adoCn = null; } if (adoCn == null) { string sConStr = ""; sConStr=System.Configuration.ConfigurationManager.AppSettings["FJPConnectionString"].ToString(); adoCn = new SqlConnection(sConStr); if (adoCn.State == ConnectionState.Open) { adoCn.Close(); } adoCn.Open(); HttpContext.Current.Session["new clsDataBase().funGetConntion()"] = adoCn; return true; } else { return false; } } catch (Exception ex) { new clsBaseNormal().funWriteErrorLog(ex); return false; } } string cmdstr; //数据库连接 protected SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strProvider"]);cmdstr = "SELECT user,name FROM 168 "; SqlDataReader reader = u1000.Data_Reader(cmdstr); if (reader.Read() == true) { Session["newmsgsum"] = reader["sum"].ToString(); Page.RegisterStartupScript("myscript", "<script>myfun()</script>"); reader.Close(); }public SqlDataReader Data_Reader(string cmdstr) { try { if (conn.State.ToString() == "Closed") { conn.Open(); } SqlCommand command = new SqlCommand(cmdstr, conn); SqlDataReader reader = command.ExecuteReader(); return reader; } catch (Exception ex) { throw new Exception("Data_Reader出错!", ex); } } 是不是 我的问题 没写清楚 主要要的是页面 连接数据库的实例<appSettings><add key="HumanResources" value="server=(localhost);Connection Timeout=30;database=HumanTables;uid=;pwd="/> </appSettings>这个web.config有了 那index.aspx读取的实例呢?不创建连接对象么 不打开连接对象么 SQL语句呢 string conString=ConfigurationManager.AppSettings["HumanResources"]); sqlconnection con=new sqlconnection(conString);con.open(); <add key="ConnString" value="Data Source=(local);Database=its;User id=sa;PWD=sa" /> web.config:<add key="conn" value="server=(localhost);Connection Timeout=30;database=job168;uid=sa;pwd=i"/>index.aspx:SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conn"]); con.Open; string cmdstr = "SELECT * FROM user"; SqlCommand command = new SqlCommand(cmdstr,con); SqlDataReader reader = command.ExecuteReader(); Response.Write(reader["id"]);为什么说 con.open 有问题?? TreeView状态问题 又是正则表达式 .NET 系统和 Java系统的数据交互问题 在线等待答案 ~~ 导航条 分页的存储过程 发布网站不成功 命名空间“System.Data”中不存在类型或命名空间名称“Sqlclient”(是缺少程序集引用吗?) 文字显示问题 (ASP高手)IIS5.1怎么样安装才能成功啊?? 急求vs2003的在线编译工具,最好交代一下怎么用,越简单的越好,在线等 String.Format()输出的时候为什么要循环两次呢?
<add key="strProvider" value="data source=px14;initial catalog=gxy_new;persist security info=False;user id =sa;workstation id=YJZ;packet size=4096;pwd=123456;"/>
数据库:168 表:users 字段:name 字段:word 在index.aspx里 用datareader读取 并输出这两个字段
<add key="HumanResources" value="server=(localhost);Connection Timeout=30;database=HumanTables;uid=;pwd="/>
</appSettings>
<add key="FJPConnectionString" value="Data Source=192.168.1.1;Initial Catalog=数据库;Persist Security Info=True;User ID=sa;Password=密码"></add>
</appSettings>
public bool funOpenCn()
{
try {
SqlConnection adoCn = (SqlConnection)HttpContext.Current.Session["new clsDataBase().funGetConntion()"];
if (adoCn != null)
{
if (adoCn.State == ConnectionState.Open)
{
adoCn.Close();
}
adoCn = null;
}
if (adoCn == null)
{
string sConStr = "";
sConStr=System.Configuration.ConfigurationManager.AppSettings["FJPConnectionString"].ToString();
adoCn = new SqlConnection(sConStr);
if (adoCn.State == ConnectionState.Open)
{
adoCn.Close();
}
adoCn.Open();
HttpContext.Current.Session["new clsDataBase().funGetConntion()"] = adoCn;
return true;
}
else {
return false;
}
}
catch (Exception ex)
{
new clsBaseNormal().funWriteErrorLog(ex);
return false;
}
}
string cmdstr;
//数据库连接
protected SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strProvider"]);
cmdstr = "SELECT user,name FROM 168 ";
SqlDataReader reader = u1000.Data_Reader(cmdstr);
if (reader.Read() == true)
{
Session["newmsgsum"] = reader["sum"].ToString();
Page.RegisterStartupScript("myscript", "<script>myfun()</script>");
reader.Close();
}
public SqlDataReader Data_Reader(string cmdstr)
{
try
{
if (conn.State.ToString() == "Closed")
{
conn.Open();
}
SqlCommand command = new SqlCommand(cmdstr, conn);
SqlDataReader reader = command.ExecuteReader();
return reader;
}
catch (Exception ex)
{
throw new Exception("Data_Reader出错!", ex);
}
}
<add key="HumanResources" value="server=(localhost);Connection Timeout=30;database=HumanTables;uid=;pwd="/>
</appSettings>这个web.config有了 那index.aspx读取的实例呢?不创建连接对象么 不打开连接对象么 SQL语句呢
con.open();
con.Open;
string cmdstr = "SELECT * FROM user";
SqlCommand command = new SqlCommand(cmdstr,con);
SqlDataReader reader = command.ExecuteReader();
Response.Write(reader["id"]);为什么说 con.open 有问题??