【问题标题】:SqlDataAdapter Insert Database not working C# winformSqlDataAdapter 插入数据库不起作用 C# winform
【发布时间】:2016-11-08 08:59:23
【问题描述】:

我正在尝试将数据插入 SQL Server 数据库并从 C# Winforms 显示在 DatagridView 上。

我点击插入按钮没有错误显示,但没有数据被插入到数据库中,Datagridview 变成空白。

我重新调试,在datagridview中可以看到数据更新,但是数据库不在Visual Studio Server Explorer DataBase(图片1)上。

另一方面。我在单击插入按钮时设置断点,跳过第 42~46 行。直接到第 50.ex 行(图片2)。

picture1

picture2

编辑: 现在的问题是当我单击插入按钮时,datagridview 已经更新了新数据。但是数据库没有插入新数据。数据库只有两个数据。

编辑2 我更改了连接字符串 AttachDbFilename。 AttachDbFilename=C:\Vis\NO4\WindowsFormsApplication1\App_Dat‌​a\Database1.mdf; 该值可以插入数据库。

这是连接字符串:

<add name="con1" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"
        providerName="System.Data.SqlClient" />

这里是 Form_load 和 load_grid 函数

SqlConnection con;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    public Form1()
    {
        InitializeComponent();
        string connectionString = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
        con = new SqlConnection(connectionString);

    }

    private void Form1_Load(object sender, EventArgs e)
    {
       load_grid();
    }
    public void load_grid()
    {
        dt.Clear();
        SqlDataAdapter da1 = new SqlDataAdapter();
        try
        {
            string sql = "SELECT * FROM profile";
            con.Open();
            da1.SelectCommand = new SqlCommand(sql, con);
            da1.Fill(ds);
            da1.Fill(dt);
            con.Close();
            dataGridView1.DataSource = dt;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

这是插入按钮

 private void button1_Click(object sender, EventArgs e)
    {
        string ac = textBox1.Text;
        string bd = textBox2.Text;
        string sex = textBox2.Text;
        string sql = "INSERT INTO profile(ac,bd,sex)VALUES('" + ac + "','" + bd + "','" + sex + "')";
        try
        {
            con.Open();
            da = new SqlDataAdapter();
            da.InsertCommand = new SqlCommand(sql, con);
            da.InsertCommand.ExecuteNonQuery();
            da.Update(dt);
            MessageBox.Show("INsert success...!!");
            load_grid();
            con.Close();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

这是 Costumer 桌子的设计

CREATE TABLE [dbo].[profile] (
[Id]  INT           IDENTITY (1, 1) NOT NULL,
[ac]  NVARCHAR (50) NULL,
[bd]  NVARCHAR (50) NULL,
[sex] NVARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC));

不确定问题出在哪里。

【问题讨论】:

  • 你在哪里初始化了连接?我没有看到提供连接字符串?
  • 对不起!我想念它。已添加。谢谢

标签: c# mysql winforms datagridview


【解决方案1】:

图片2显示连接已打开的异常,因此请在打开连接之前尝试以下操作。您在关闭连接之前调用load_grid();。 我已更新所有代码,请按照以下说明使用:

编辑-第二次修订:

public void load_grid()
    {
        dt.Clear();
        SqlDataAdapter da1 = new SqlDataAdapter();
        try
        {
            string sql = "SELECT * FROM profile";
            con.Open();
            da1.SelectCommand = new SqlCommand(sql, con);
            da1.Fill(ds);
            da1.Fill(dt);
            con.Close();
            dataGridView1.DataSource = dt;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            if (con.State != System.Data.ConnectionState.Closed)
                con.Close();
        }
    }


 private void button1_Click(object sender, EventArgs e)
        {
            string ac = textBox1.Text;
            string bd = textBox2.Text;
            string sex = textBox2.Text;
            string sql = "INSERT INTO profile(ac,bd,sex)VALUES('" + ac + "','" + bd + "','" + sex + "')";
            try
            {
                con.Open();
                da = new SqlDataAdapter();
                da.InsertCommand = new SqlCommand(sql, con);
                da.InsertCommand.ExecuteNonQuery();
                da.Update(dt);
                con.Close();
                MessageBox.Show("INsert success...!!");
                load_grid();               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (con.State != System.Data.ConnectionState.Closed)
                    con.Close();
            }
        }

【讨论】:

  • 非常感谢。点击插入按钮后,Datagridview 已更新。但是profile表不会插入,只会test和test1。
  • 我也有疑问。为什么con没有关闭,在第一次调用load grid()函数时。
  • 如果在第一次或第二次查询中出现任何异常,它将永远无法关闭连接,直到关闭应用程序。你的代码不干净,我看到很多问题。我已更新我的答案检查,如果问题仍然存在,请告诉我。
  • 您的意思是只插入了两个值而不是 3?可能是因为您将 textBox2 文本用于两个字段? bd 和 sex 列?
  • 刚刚注意到您正在调用 load_grid(); da.Update(dt) 之后;这意味着您的连接尚未关闭。移动 con.Close();在 load_grid() 之前
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-07
  • 2014-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多