我正在制作一个c#程序,它将Windows窗体应用程序的输入转换为localDB。我浏览了关于app.config和实际调用的教程,并在这里查看了其他问题并且没有找到任何修复它的东西。具体来说,如果我不使用try / catch它会完全断开" SqlCommand cmd = conn.CreateCommand(); person newPerson = new person(FirstNameBox.Text,phoneBox.Text,emailBox.Text,LastNameBox.Text) ;" 它确实说"配置元素未声明"但我还没有看到任何表明它出错的东西?
try
{
System.Data.SqlClient.SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["database"].ConnectionString);
SqlCommand cmd = conn.CreateCommand();person newPerson = new person(FirstNameBox.Text, phoneBox.Text, emailBox.Text, LastNameBox.Text);
cmd.CommandText = @"Insert into Person (FirstName,LastName,Email,Phone)
VALUES(@FirstName, @LastName, @Email, @Phone)
";
cmd.Parameters.AddWithValue("@FirstName", newPerson.getFirstName());
cmd.Parameters.AddWithValue("@LastName", newPerson.getLastName());
cmd.Parameters.AddWithValue("@Email", newPerson.getEmail());
cmd.Parameters.AddWithValue("@Phone", newPerson.getPhone());
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
if (!ValidateForm())
{
return;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,
Application.ProductName,
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
的app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Midterm.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="database"
connectionString=" Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Matthew\Documents\Visual Studio 2012\Projects\Midterm\Midterm\Database1.mdf;Integrated Security=True;pooling=false"
providerName="System.Data.SqlClient" ></add>
</connectionStrings>
<connectionStrings>
<add name="Midterm.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
答案 0 :(得分:1)
您有两个configurationStrings部分。你只需要一个
<connectionStrings>
<add name="database" connectionString="......."/>
<add name="Midterm.Properties.Settings.Database1ConnectionString" connectionString="..... " />
</connectionStrings>
答案 1 :(得分:0)
配置文件中有两个不同的<connectionStrings>
节点
<connectionStrings>
<add name="database"
connectionString=" Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Matthew\Documents\Visual Studio 2012\Projects\Midterm\Midterm\Database1.mdf;Integrated Security=True;pooling=false"
providerName="System.Data.SqlClient" ></add>
</connectionStrings>
<connectionStrings>
<add name="Midterm.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
将它们合并为一个<connectionStrings>
条目。
<connectionStrings>
<add name="database"
connectionString=" Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Matthew\Documents\Visual Studio 2012\Projects\Midterm\Midterm\Database1.mdf;Integrated Security=True;pooling=false"
providerName="System.Data.SqlClient" />
<add name="Midterm.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>