本来是好用的,但把数据库的操作写到单独的类文件中就提示:ConnectionString 属性尚未初始化~
代码有点多,麻烦了各位了~~~~
web.config代码
<configuration> <appSettings> <add key="sqlconn" value="server=localhost;database=data;uid=sa;pwd=sa"/> </appSettings> <connectionStrings> <add name="DataConnectionString" connectionString="Data Source=SUN;Initial Catalog=Data;User ID=sa;Password=sa" providerName="System.Data.SqlClient" /> </connectionStrings>
namespace DB{ public class DbOprate { private string sqlconn=null; public DbOprate() { string sqlconn = ConfigurationManager.AppSettings["sqlconn"].ToString(); } //创建数据库链接,返回myconnection; public SqlConnection myconnection() { SqlConnection myconnection = new SqlConnection(sqlconn); myconnection.Open(); return myconnection; } //释放资源,断开链接; public void Dispose(SqlConnection myconnection) { if (myconnection != null) { myconnection.Close(); myconnection.Dispose(); } GC.Collect(); } //创建新的SQL命令对象(存储过程) private SqlCommand CreateCmd(string procName, IDataParameter[] prams) { SqlConnection myconnection; myconnection = new SqlConnection(sqlconn); myconnection.Open(); SqlCommand mycommand = new SqlCommand(procName, myconnection); mycommand.CommandType = CommandType.StoredProcedure; if (prams != null) { foreach (SqlParameter parameter in prams) { if (parameter != null) { mycommand.Parameters.Add(parameter); } } } return mycommand; } //执行存储过程:无返回值 public void ExecuteProcedure(string procName, IDataParameter[] prams) { SqlConnection myconnection; myconnection = new SqlConnection(sqlconn); myconnection.Open(); SqlCommand mycommand; mycommand = CreateCmd(procName, prams); mycommand.ExecuteNonQuery(); myconnection.Close(); } }}
protected void btnSave_Click(object sender, EventArgs e) { DbClass = new DB.DbOprate(); myconnection = DbClass.myconnection(); SqlParameter[] myparams=new SqlParameter [4]; myparams[0] = new SqlParameter("@UserId", SqlDbType.VarChar, 50); myparams[1] = new SqlParameter("PassWord", SqlDbType.VarChar, 50); myparams[2] = new SqlParameter("@Roles", SqlDbType.VarChar, 50); myparams[3] = new SqlParameter("@Sex", SqlDbType.Int, 4); myparams[0].Value = txtUserId.Text; myparams[1].Value = txtPassWord.Text; myparams[2].Value = ddlRoles.SelectedItem.Text; myparams[3].Value = ddlSex.SelectedValue.ToString(); DbClass.ExecuteProcedure("UserAdd", myparams); Response.Write("<script>window.alert('保存成功!!!')</script>"); }
string sqlconn = ConfigurationManager.AppSettings["sqlconn"].ToString();
改成
string sqlconn = ConfigurationManager.ConnectionStrings["sqlconn"];
试下
[解决办法]
晕。 这是啥?
protected void btnSave_Click(object sender, EventArgs e)
{
DbClass = new DB.DbOprate();
myconnection = DbClass.myconnection();
SqlParameter[] myparams=new SqlParameter [4];
}
[解决办法]
<configuration>
<appSettings>
<add key="sqlconn" value="server=localhost;database=data;uid=sa;pwd=sa"/>
</appSettings>
<connectionStrings>
<add name="DataConnectionString" connectionString="Data Source=SUN;Initial Catalog=Data;User ID=sa;Password=sa"
providerName="System.Data.SqlClient" />
</connectionStrings>
这里怎么配置2个。如果是用vs2005,建议去掉
<configuration>
<appSettings>
<add key="sqlconn" value="server=localhost;database=data;uid=sa;pwd=sa"/>
</appSettings>
保留 <connectionStrings>就可以了。 看看sun 是不是服务器名字。如果是本地还是改为connectionString="Data Source=127.0.0.1;吧。
如果是vs2005,应该是:string sqlconn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
并且要添加引入Configuration这个。
[解决办法]
private string sqlconn=null;
public DbOprate()
{
string sqlconn = ConfigurationManager.AppSettings["sqlconn"].ToString();
}
//创建数据库链接,返回myconnection;
public SqlConnection myconnection()
{
SqlConnection myconnection = new SqlConnection(sqlconn);
myconnection.Open();
return myconnection;
}
问题在这里吧 你在上边定义的和在myconnection()中用到的sqlconn不是一个