当我将我的程序硬编码为与数据库连接时,它工作正常,但当我去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();
}
}
}
我没有连接到数据库。
你们能解决这个问题......我已经尝试了一百次
答案 0 :(得分:2)
请在app.config文件中写下以下代码。
<Configuration>
<connectionStrings>
<add name="AppName" connectionString="Connectionstring Name" />
</connectionStrings>
</Configuration>
请在.cs文件中写下以下代码。这个类文件对所有人都是通用的。
public string connection()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["AppName"].ToString();
}
答案 1 :(得分:0)
添加连接字符串的方式似乎是错误的。您应该按以下方式进行:
<connectionStrings>
<add name="MyConnection" connectionString="Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp" providerName="System.Data...."/>
</connectionStrings>
答案 2 :(得分:0)
您必须使用ConfigurationManager.AppSettings[""]
代替ConfigurationSettings.AppSettings["ConnectionString"])
现在不推荐使用ConfigurationSetting。
答案 3 :(得分:0)
首先,您的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();
}
}
}