当前位置: 代码迷 >> ASP.NET >> 本来是好用的,但把数据库的操作写到独自的类文件中就提示:ConnectionString 属性尚未初始化
  详细解决方案

本来是好用的,但把数据库的操作写到独自的类文件中就提示:ConnectionString 属性尚未初始化

热度:1524   发布时间:2013-02-25 00:00:00.0
本来是好用的,但把数据库的操作写到单独的类文件中就提示:ConnectionString 属性尚未初始化~
代码有点多,麻烦了各位了~~~~
web.config代码
C# code
<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>

类文件代码
---------------------------------------------------------------------------
C# code
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();                    }    }}

调用代码:
------------------------------------------------------------------------------------------------
C# code
  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>");            }


------解决方案--------------------------------------------------------