[英]App.Config is not working
當我的程序硬編碼與數據庫連接,它的工作正常,但當我去aap.config文件...我沒有連接到數據庫。
在我的app.config文件中
<add key="ConnectionString"
value="Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp"/>
當我對我的代碼進行硬編碼時,我就像這樣連接了
public void BindDBDropDown()
{
SqlConnection sConnection = new SqlConnection("Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp");
//To Open the connection.
sConnection.Open();
string selectDatabase = @"SELECT NAME FROM master..sysdatabases";
SqlCommand sCommand = new SqlCommand(selectDatabase, sConnection);
try
{
DataSet dsListOfDatabases = new DataSet("master..sysdatabases");
SqlDataAdapter da = new SqlDataAdapter(selectDatabase, sConnection);
da.TableMappings.Add("Table", "master..sysdatabases");
da.Fill(dsListOfDatabases);
DataViewManager dsv = dsListOfDatabases.DefaultViewManager;
cmbDatabases.DataSource = dsListOfDatabases.Tables["master..sysdatabases"];
cmbDatabases.DisplayMember = "NAME";
cmbDatabases.ValueMember = ("");
}
catch (Exception ex)
{
//All the exceptions are handled and written in the EventLog.
EventLog log = new EventLog("Application");
log.Source = "MFDBAnalyser";
log.WriteEntry(ex.Message);
}
finally
{
if (sConnection.State != ConnectionState.Closed)
{
sConnection.Close();
}
}
}
但是當我使用的時候
public static DataSet GetPrimaryKeyTables()
{
SqlConnection sConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
string selectPrimaryKeys;
//Opens the connection
sConnection.Open();
selectPrimaryKeys = @"SELECT [TABLE_NAME]
FROM [INFORMATION_SCHEMA.TABLE_CONSTRAINTS]
WHERE [CONSTRAINT_TYPE = 'PRIMARY KEY']
ORDER BY [TABLE_NAME]";
SqlCommand sCommand = new SqlCommand(selectPrimaryKeys, sConnection);
try
{
DataSet dtPrimaryKeysTables = new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS");
SqlDataAdapter da = new SqlDataAdapter(selectPrimaryKeys, sConnection);
da.TableMappings.Add("Table", "INFORMATION_SCHEMA.TABLE_CONSTRAINTS");
da.Fill(dtPrimaryKeysTables);
DataViewManager dsva = dtPrimaryKeysTables.DefaultViewManager;
return dtPrimaryKeysTables;
}
catch (Exception ex)
{
//All the exceptions are handled and written in the EventLog.
EventLog log = new EventLog("Application");
log.Source = "MFDBAnalyser";
log.WriteEntry(ex.Message);
return null;
}
finally
{
//To close the connection
if (sConnection != null)
{
sConnection.Close();
}
}
}
我沒有連接到數據庫。
你們可以解決這個問題......我已經嘗試了一百次
請在app.config文件中編寫以下代碼。
<Configuration>
<connectionStrings>
<add name="AppName" connectionString="Connectionstring Name" />
</connectionStrings>
</Configuration>
請在.cs文件中寫下以下代碼。 這個類文件對所有人都是通用的。
public string connection()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["AppName"].ToString();
}
添加連接字符串的方法似乎是錯誤的。 你可以用以下方式做到這一點:
<connectionStrings>
<add name="MyConnection" connectionString="Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp" providerName="System.Data...."/>
</connectionStrings>
您必須使用ConfigurationManager.AppSettings[""]
而不是ConfigurationSettings.AppSettings["ConnectionString"])
如今不建議使用ConfigurationSettings。
首先,您的app.config應該如下所示:
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=192.168.10.3;Initial Catalog=GoalPlanNew;User Id=gp;Password=gp;" providerName="System.Data.SqlClient"/>
</connectionStrings>
然后添加對System.Configuration命名空間的引用。
然后修改你的代碼:
public static DataSet GetPrimaryKeyTables()
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection sConnection = new SqlConnection(connectionString);
string selectPrimaryKeys;
//Opens the connection
sConnection.Open();
selectPrimaryKeys = @"SELECT [TABLE_NAME]
FROM [INFORMATION_SCHEMA.TABLE_CONSTRAINTS]
WHERE [CONSTRAINT_TYPE = 'PRIMARY KEY']
ORDER BY [TABLE_NAME]";
SqlCommand sCommand = new SqlCommand(selectPrimaryKeys, sConnection);
try
{
DataSet dtPrimaryKeysTables = new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS");
SqlDataAdapter da = new SqlDataAdapter(selectPrimaryKeys, sConnection);
da.TableMappings.Add("Table", "INFORMATION_SCHEMA.TABLE_CONSTRAINTS");
da.Fill(dtPrimaryKeysTables);
DataViewManager dsva = dtPrimaryKeysTables.DefaultViewManager;
return dtPrimaryKeysTables;
}
catch (Exception ex)
{
//All the exceptions are handled and written in the EventLog.
EventLog log = new EventLog("Application");
log.Source = "MFDBAnalyser";
log.WriteEntry(ex.Message);
return null;
}
finally
{
//To close the connection
if (sConnection != null)
{
sConnection.Close();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.