简体   繁体   English

在C#中将值插入数据库中的表

[英]Insert values to table in database in c#

I have a Winforms application and database to save data from user. 我有一个Winforms应用程序和数据库来保存用户数据。

When I insert data everything works fine but when I clean the solution and load the GUI of the database to see the old data.. I don't see the datam the datagridview is empty. 当我插入数据时,一切正常,但是当我清理解决方案并加载数据库的GUI以查看旧数据时。.我看不到datam,datagridview为空。

using (SqlConnection con = new SqlConnection(dataBase.Connection.ConnectionString))
{
   using (SqlCommand wow = new SqlCommand("insert into GamesTbl(Type,Date,Time) Values(@type,@date,@time)", con))
   {
      wow.Parameters.AddWithValue("@type", "vsPC");
      wow.Parameters.AddWithValue("@date", DateTime.Now.Date);
      wow.Parameters.AddWithValue("@time", DateTime.Now.TimeOfDay);

      try
      {
          con.Open();
          wow.ExecuteNonQuery();
          con.Close();
      }
      catch (Exception ex)
      {
         MessageBox.Show(ex.Message);
      }
   }
}

What is wrong? 怎么了?

EDIT: binding data on DBGui_load 编辑:在DBGui_load上绑定数据

private void DBGui_Load(object sender, EventArgs e)
    {
        dataGridView1.DataSource = playersTblBindingSource;
        playersTblBindingSource.DataSource = DB.GamesTbls;

    }

EDIT: my connection string: 编辑:我的连接字符串:

"Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated     
Security=True;User Instance=True"

The whole User Instance and AttachDbFileName= approach is flawed - at best! 整个用户实例和AttachDbFileName =方法都有缺陷-充其量! When running your app in Visual Studio, it will be copying around the .mdf file (to the output directory - typically .\\bin\\debug - where you app runs) and most likely , your INSERT works just fine - but you're just looking at the wrong .mdf file in the end! 在Visual Studio中运行您的应用程序时,它将在.mdf文件周围复制(到应用程序运行所在的输出目录-通常是.\\bin\\debug ),并且很可能您的INSERT工作正常-但您只是最终看到了错误的.mdf文件

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there. 如果您要坚持这种方法,请尝试在myConnection.Close()调用上放置一个断点,然后使用SQL Server Mgmt Studio Express检查.mdf文件-我几乎可以确定您的数据在那里。

The real solution in my opinion would be to 我认为真正的解决方案

  1. install SQL Server Express (and you've already done that anyway) 安装SQL Server Express(并且您已经完成了此操作)

  2. install SQL Server Management Studio Express 安装SQL Server Management Studio Express

  3. create your database in SSMS Express , give it a logical name (eg Database1 - or while you're at it - give it a more sensible name...) SSMS Express中创建数据库,给它一个逻辑名称(例如Database1或在您使用它时-给它一个更合理的名称...)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. 使用其逻辑数据库名称 (在服务器上创建时提供)连接到该数据库 -并且不要弄乱物理数据库文件和用户实例。 In that case, your connection string would be something like: 在这种情况下,您的连接字符串将类似于:

     Data Source=.\\\\SQLEXPRESS;Database=YourDatabase;Integrated Security=True 

    and everything else is exactly the same as before... 和其他一切是完全一样的前...

Seems liks you are missing DataBind . 好像您缺少DataBind

private void DBGui_Load(object sender, EventArgs e)
{
    dataGridView1.DataSource = playersTblBindingSource;
    dataGridView1.DataBind(); // you are missing this
    playersTblBindingSource.DataSource = DB.GamesTbls;
}

As an alternative solution, what you can do to fix the problem is to set the property of your database file in your solution as follows 作为替代解决方案,可以解决此问题的方法是,按如下所示在解决方案中设置数据库文件的属性

Copy To Output Directory : do not copy Copy To Output Directorydo not copy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM