wpf C#:将值插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26444679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: Inserting values into database
提问by Seda
I′ve WPF application where i have button SaveData which calls function InsertValuesIntoGAStatistics and i want to insert data into database but nothing is inserted i don′t know where is problem everything looks fine and i didn′t get any error message just nothing is done.
我有 WPF 应用程序,我有按钮 SaveData 调用函数 InsertValuesIntoGAStatistics,我想将数据插入数据库但没有插入任何内容我不知道问题出在哪里一切看起来都很好,我没有收到任何错误消息只是什么也没做.
private void SaveData_Click(object sender, RoutedEventArgs e)
{
database.InsertValuesIntoGAStatistics("haha", 4, 4, 4, 4, 11, "hlalha", "blabla", "blabsdla");
}
button for saving data
用于保存数据的按钮
public static string ConnectionStringGADatabase
{
get
{
return ConfigurationManager.ConnectionStrings["GADatabase"].ConnectionString;
}
}
here is property to get connectionString
这是获取 connectionString 的属性
public void InsertValuesIntoGAStatistics(string GAType, int Generations, int PopulationSize, float MaxFitness, float AverageFitness, int DmaxValue, string Selection, string CrossOver, string FilePath)
{
try
{
using (SqlConnection connection = new SqlConnection(Connection.ConnectionStringGADatabase))
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "INSERT INTO GAStatistics (GAType, Generations, PopulationSize, MaxFitness, AverageFitness, DmaxValue, Selection, CrossOver, FilePath) VALUES (@GAType, @Generations, @PopulationSize, @MaxFitness, @AverageFitness, @DmaxValue, @Selection, @CrossOver, @FilePath)";
command.Parameters.AddWithValue("@GAType", GAType);
command.Parameters.AddWithValue("@Generations", Generations);
command.Parameters.AddWithValue("@PopulationSize", PopulationSize);
command.Parameters.AddWithValue("@MaxFitness", MaxFitness);
.............
connection.Open();
command.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
}
this is function InsertValuesIntoGAStatistics which should insert data into database
这是应该将数据插入数据库的函数 InsertValuesIntoGAStatistics
<connectionStrings>
<add name="GADatabase" connectionString="Data Source=LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database\GADatabase.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
and this part of code is in App.config file
这部分代码在 App.config 文件中
回答by Eru
First of all if you have copied the app.config file, then you have an error in it... you are missing the opening bracket - apart from that the config looks fine to me.
首先,如果你已经复制了 app.config 文件,那么你有一个错误......你缺少左括号 - 除此之外,配置对我来说很好。
Second: Try to put symbol @ before "insert into..." so it will look like this:
第二:尝试在“插入...”之前放置符号@,因此它看起来像这样:
command.CommandText = @"INSERT INTO GAStatistics (GAType, Generations, PopulationSize, MaxFitness, AverageFitness, DmaxValue, Selection, CrossOver, FilePath) VALUES (@GAType, @Generations, @PopulationSize, @MaxFitness, @AverageFitness, @DmaxValue, @Selection, @CrossOver, @FilePath)";

