一个棘手的问题,解决不了,无法进行下一个环节,急求各位大虾帮帮忙,让小菜度过“难关”!我在web.config中如下写:
<appSettings/>
<connectionStrings>
<add name="MyConn" connectionString="Data Source=CHINA-129BD748C\SQLEXPRESS;Initial Catalog=PeopleManageSystem;Integrated Security=sspi"/>
</connectionStrings>
而在调用数据库中数据时,连接方法出现异常,具体如下:
con.Open();
用户代码未处理InvalidOperationException
ConnectionString属性尚未初始化与数据库存储过程连接的具体代码如下:
public string[] Login(string loginid, string password)
{
string[] CheckLogin;
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["MyConn"]);
SqlCommand cmd = new SqlCommand("sysEmpLogin", con);
cmd.CommandType = CommandType.StoredProcedure; SqlParameter parameterID = new SqlParameter("@loginid", SqlDbType.VarChar, 50);
parameterID.Value = loginid;
cmd.Parameters.Add(parameterID); SqlParameter parameterPassword = new SqlParameter("@password", SqlDbType.VarChar, 50);
parameterPassword.Value = password;
cmd.Parameters.Add(parameterPassword); SqlParameter parameterEmpID = new SqlParameter("@empid", SqlDbType.VarChar, 50);
parameterEmpID.Direction =ParameterDirection.Output;
cmd.Parameters.Add(parameterEmpID); SqlParameter parameterReason = new SqlParameter("@reason", SqlDbType.VarChar, 50);
parameterReason.Direction=ParameterDirection.Output;
cmd.Parameters.Add(parameterReason); con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
拜托各位,帮忙找找解决办法,先谢过
<appSettings/>
<connectionStrings>
<add name="MyConn" connectionString="Data Source=CHINA-129BD748C\SQLEXPRESS;Initial Catalog=PeopleManageSystem;Integrated Security=sspi"/>
</connectionStrings>
而在调用数据库中数据时,连接方法出现异常,具体如下:
con.Open();
用户代码未处理InvalidOperationException
ConnectionString属性尚未初始化与数据库存储过程连接的具体代码如下:
public string[] Login(string loginid, string password)
{
string[] CheckLogin;
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["MyConn"]);
SqlCommand cmd = new SqlCommand("sysEmpLogin", con);
cmd.CommandType = CommandType.StoredProcedure; SqlParameter parameterID = new SqlParameter("@loginid", SqlDbType.VarChar, 50);
parameterID.Value = loginid;
cmd.Parameters.Add(parameterID); SqlParameter parameterPassword = new SqlParameter("@password", SqlDbType.VarChar, 50);
parameterPassword.Value = password;
cmd.Parameters.Add(parameterPassword); SqlParameter parameterEmpID = new SqlParameter("@empid", SqlDbType.VarChar, 50);
parameterEmpID.Direction =ParameterDirection.Output;
cmd.Parameters.Add(parameterEmpID); SqlParameter parameterReason = new SqlParameter("@reason", SqlDbType.VarChar, 50);
parameterReason.Direction=ParameterDirection.Output;
cmd.Parameters.Add(parameterReason); con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
拜托各位,帮忙找找解决办法,先谢过
解决方案 »
- 如何自动更新缓存?
- append动态生成table,数据保存到数据库
- Ajax技术的主要目的和怎样应用
- 怎么给一个asp.net组件添加缓存
- SQL2000查询结果保存为XML文件时的一个细节?
- 重新提问(在线等!)
- 谁可以从内存分配的角度分析(什么时候用静态方法好,什么时候不该用静态方法?)[讨论这个问题还需要给分吗?]
- 为什么用存储过程会比sql语句还慢~
- 在vs.net中设计类型化数据集,如何定义一个字段为Guid类型?
- 如何在调用一个方法中一次性返回三个对象变量???
- 获取其它网站的title,显示乱码
- asp.net头部尾部用母版页与#include导入头尾有什么区别?求机制分析,万谢
因此config文件中要将MyConn放到appSettings中。<appSettings><add name="MyConn" connectionString="Data Source=CHINA-129BD748C\SQLEXPRESS;Initial Catalog=PeopleManageSystem;Integrated Security=sspi"/>
</appSettings><connectionStrings>
</connectionStrings>
{
conn.Open();
SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
MyCommand.CommandType = CommandType.StoredProcedure;
MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int));
MyCommand.Parameters["@a"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int));
MyCommand.Parameters["@b"].Direction = ParameterDirection.Output;
MyCommand.ExecuteNonQuery();
Response.Write(MyCommand.Parameters["@b"].Value.ToString());
}
<add name="MyConn" connectionString="Data Source=CHINA-129BD748C\SQLEXPRESS;Initial Catalog=PeopleManageSystem;Integrated Security=sspi"/>
</appSettings>
大概有两种形式
<add key="" value /?
<add name="" connectonstring="" /?
这两方法访问方式不一样的
第一种:System.Configuration.ConfigurationManager.AppSettings["MyConn"]
第二种:ConfigurationManager.ConnectionStrings["MyConn"]
你自己对应一下
string mycon=ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
SqlConnection con=new SqlConnection(mycon);
问题就解决了,谢谢各位!