连接字符串属性尚未初始化
我不断收到错误消息
连接字符串属性尚未初始化
我搜索了一个小时,尝试将解决方案应用于我的项目,但是它不起作用
我正在将错误日志插入到sql表中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace ErrorLog
{
internal class LogToDB : ILog
{
static string connection_string = ConfigurationSettings.AppSettings["Sever"];
public void logNow(string strError)
{
SqlConnection conn = new SqlConnection(connection_string);
List<string> errors = new List<string>();
try
{
string strSQL = "insert_errolog";
SqlDataAdapter mySA = new SqlDataAdapter(strSQL, conn);
mySA.SelectCommand.CommandType = CommandType.StoredProcedure;
mySA.SelectCommand.Parameters.Add(new SqlParameter("@errorMsg", SqlDbType.VarChar, 50));
mySA.SelectCommand.Parameters["@errorMsg"].Value = strError;
DataSet myDS = new DataSet();
mySA.Fill(myDS);
}
catch (Exception e)
{
errors.Add("Error: " + e.ToString());
}
finally
{
conn.Dispose();
conn = null;
}
}
}
}
,这是App.config
file
<configuration>
<appSettings>
<add key="Destination" value="ToSQL"/>
<add key="Sever" value="Data Source=MYSQLSEVER ;Initial Catalog=Myproject;User Id=test1;Password=12345"/>
</appSettings>
</configuration>
当我插入数据时,它抛出异常:
连接字符串属性尚未初始化
有人看到我的代码中的问题吗?
I keep getting the error
the connectionstring property has not been initialized
I was searching for an hour and i tried to apply solution to my project but, it did not work
I am inserting error log to sql table
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace ErrorLog
{
internal class LogToDB : ILog
{
static string connection_string = ConfigurationSettings.AppSettings["Sever"];
public void logNow(string strError)
{
SqlConnection conn = new SqlConnection(connection_string);
List<string> errors = new List<string>();
try
{
string strSQL = "insert_errolog";
SqlDataAdapter mySA = new SqlDataAdapter(strSQL, conn);
mySA.SelectCommand.CommandType = CommandType.StoredProcedure;
mySA.SelectCommand.Parameters.Add(new SqlParameter("@errorMsg", SqlDbType.VarChar, 50));
mySA.SelectCommand.Parameters["@errorMsg"].Value = strError;
DataSet myDS = new DataSet();
mySA.Fill(myDS);
}
catch (Exception e)
{
errors.Add("Error: " + e.ToString());
}
finally
{
conn.Dispose();
conn = null;
}
}
}
}
and this is the App.config
file
<configuration>
<appSettings>
<add key="Destination" value="ToSQL"/>
<add key="Sever" value="Data Source=MYSQLSEVER ;Initial Catalog=Myproject;User Id=test1;Password=12345"/>
</appSettings>
</configuration>
When I inserted data, it was throwing an exception:
The connectionstring property has not been initialized
Does anyone see the problem in my code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用这样的连接字符串(在 app.config 中):
You can use connection string like this (in app.config):
你没有打开你的连接。
You're not opening your connection.
尝试对您的连接字符串进行编码,以确认其是否有效
将此行替换为
静态字符串connection_string = ConfigurationSettings.AppSettings["Sever"];
看看这是否有效,如果有效,则说明您的连接字符串值未正确从应用程序设置中获取。
Try harcoding your connection string, to confirm if its working or no
Replace this line with
static string connection_string = ConfigurationSettings.AppSettings["Sever"];
See if that works, if it does, then your connection string value is not fetched from appsettings correctly.
Net Developer,
您可以将代码转换为 c#
您应该这样做,
创建一个全局连接变量
,它将解决您的连接属性未初始化的问题
希望它有所帮助。
Net Developer,
you can convert the code in c#
You should do it like this
make a global connection variable
it will solve your connection property not initialized problem
hope it helps.
在这行代码中
static string connection_string = ConfigurationSettings.AppSettings["Sever"];
您应该编写“
connectionstring
”而不是“Server
”。In this line of code
static string connection_string = ConfigurationSettings.AppSettings["Sever"];
you should write "
connectionstring
" instead of "Server
".