ConnectionString属性尚未初始化
我正在寻找一个小时,我尝试应用解决方案,我的项目错误,但,它没有工作
我将错误日志,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
文件
<configuration>
<appSettings>
<add key="Destination" value="ToSQL"/>
<add key="Sever" value="Data Source=MYSQLSEVER ;Initial Catalog=Myproject;User Id=test1;Password=12345"/>
</appSettings>
</configuration>
当我插入的数据,它抛出一个异常:
ConnectionString属性尚未初始化
有谁看到了问题在我的代码?
为什么您使用'appSettings'而不是现有的'connectionStrings'配置节? – Oded
确定MYSQLSEVER后应该有空格吗? – beny23
你真的打电话给你的服务器'SEVER'(或者你错过了一个'R'来让它成为'SERVER'吗?) –