我正在尝试将数据插入到SQL Server数据库中,并在C#Winforms上显示在DatagridView上。
我单击“插入”按钮没有显示错误,但没有数据插入数据库,Datagridview变为空白。
我重新调试可以看到datagridview中的数据更新,但数据库不在Visual Studio Server Explorer数据库(picture1)上。
另一方面。我点击插入按钮时设置断点,跳过42~46行。直接到50.ex(图2)。
修改: 现在的问题是当我点击插入按钮时,datagridview有更新新数据。但是数据库没有插入新数据。数据库只有两个数据。
EDIT2
我更改了连接字符串AttachDbFilename。
AttachDbFilename=C:\Vis\NO4\WindowsFormsApplication1\App_Data\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));
不确定问题出在哪里。
答案 0 :(得分:0)
Picture2显示连接已打开的异常,请在打开连接之前尝试按照。在关闭连接之前,您正在调用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();
}
}