我正在研究一个学校项目,该项目需要能够将文本框中的数据放入数据库中。我可以将数据添加到列表框中,并且我已连接到数据库,但是当我刷新它们时,新名称不会弹出数据库。有人可以帮我吗?下面列出了代码,提前谢谢!
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace KillerApp_Calorie
{
public partial class Form1 : Form
{
string connectionString;
public Form1()
{
InitializeComponent();
connectionString = ConfigurationManager.ConnectionStrings["KillerApp_Calorie.Properties.Settings.DBCalorieAppConnectionString"].ConnectionString;
}
private List<Persoon> personen = new List<Persoon>();
private List<Gerecht> gerechten = new List<Gerecht>();
private void Form1_Load(object sender, EventArgs e)
{
/* using (connection = new SqlConnection(connectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM
Persoon", connection))
{
connection.Open();
DataTable PersoonTable = new DataTable();
adapter.Fill(PersoonTable);
lb_gebruikers.DisplayMember = "Naam";
lb_gebruikers.ValueMember = "Id";
lb_gebruikers.DataSource = PersoonTable;
} */
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DBCalorieApp.mdf;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(); // you can define
commandText and connection in SqlCommand(defineArea);
cmd.Connection = con; // like; cmd =
newSqlCommand("Insert into...",con);
string name = tb_naam.Text;
string gender = tb_geslacht.Text;
cmd.CommandText = "Insert into
Persoon(Naam,Geslacht)values('" + name + "','" + gender + "')";
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
lb_gebruikers.Items.Add(name + " - " + gender);
}
catch (Exception ex)
{
MessageBox.Show("Exception : " + ex);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(@"Data Source=
(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DBCalorieApp.mdf;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(); // you can define
commandText and connection in SqlCommand(defineArea);
cmd.Connection = con; // like; cmd =
newSqlCommand("Insert into...",con);
string gerecht = tb_gerecht.Text;
string cal = tb_cal.Text;
string inh = tb_inhoud.Text;
cmd.CommandText = "Insert into Gerechten(Gerecht,Calorieën,
Inhoud)values('" + gerecht + "','" + cal + "', '" + inh +"')";
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
lb_gerechten.Items.Add(gerecht + " - " + cal);
}
catch (Exception ex)
{
MessageBox.Show("Exception : " + ex);
}
}
private void button3_Click(object sender, EventArgs e)
{
gerechten.Clear();
lb_gerechten.DataSource = null;
//lb_gerechten.Items.Equals("");
}
}
}
答案 0 :(得分:0)
整个AttachDbFileName=
方法存在缺陷 - 充其量!在Visual Studio中运行应用程序时,它将复制.mdf
文件(从App_Data
目录到输出目录 - 通常是.\bin\debug
- 应用程序运行的地方)和最有可能,您的INSERT
工作正常 - 但您最后只是查看错误的.mdf文件!
如果你想坚持这种方法,那么尝试在myConnection.Close()
调用上设置一个断点 - 然后用SQL Server Mgmt Studio Express检查.mdf
文件 - 我几乎可以肯定你的数据就在那里。
我认为真正的解决方案将是
安装SQL Server Express(无论如何你已经完成了)
安装SQL Server Management Studio Express
在 SSMS Express 中创建数据库,为其指定一个逻辑名称(例如DBCalorieApp
)
使用其逻辑数据库名称(在服务器上创建时给定)连接到它 - 并且不要乱用物理数据库文件和用户实例。在这种情况下,您的连接字符串将类似于:
Data Source=.\\SQLEXPRESS;Database=DBCalorieApp;Integrated Security=True
其他所有内容都完全与以前相同......
另请参阅Aaron Bertrand的优秀博客文章Bad habits to kick: using AttachDbFileName以获取更多背景信息。