剛剛更改爲SQL Server數據庫,並試圖訪問我的web.config中設置的連接字符串。我收到以下錯誤:SQL Server ConnectionString錯誤
ConnectionString屬性尚未初始化。
的Web.Config代碼:
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="EditorGenerator.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="loganWebConn" connectionString="Data Source=PMCUSSRV05;Integrated Security=true;Initial Catalog= loganWeb;User Id=logan_sys;Password=appPwd;" providerName="System.Data.SqlClient" />
</connectionStrings>
C#代碼:
SqlConnection con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["loganWebConn"].ConnectionString;
con.Open();
try
{
string sql = "select j.*, u.fullname as plannerName from logan_jobs j left join on logan_user u on u.userID = j.plannerID ";
sql += "where 0=0 ";
sql += "and plannerID =" + planner + " ";
sql += "and " + dept + " in (select deptID from logan_prodData p where p.jobID = j.jobID and curEstEndDate >= " + fromDate + " and curEstEndDate <= " + toDate + ") ";
sql += "order by jobNumber";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
我在做什麼錯?我知道這是真正的基本東西...
爲什麼用0 = 0打擾?它根本沒有任何好處。更重要的是您創建查詢的方式。您需要閱讀,瞭解並開始使用參數化查詢。這段代碼對於sql注入是開放的。 –
哦,你也有一些邏輯問題,因爲你沒有參數化你的查詢。你沒有在你的日期使用單引號,所以會發生什麼情況是數學將根據字符串的格式作爲分割或者減法來執行。那些新的整數值將被隱含地轉換爲日期,並且不會做你認爲它將要做的事。最後但並非最不重要的一點,您應該繼續使用相同的字符串或開始使用StringBuilder類。 –
所以當你調試你的代碼時,從配置文件加載後'con'對連接字符串有什麼價值? – DeanOC