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'嗎?) –