c#Datagridview修改
在datagridview修改了数据,但数据库没有变化,是我SQL语句写错了吗?下面试代码。主form传递dataset来当前页面。最好留个联系方式,谢谢!usingSy...
在datagridview修改了数据,但数据库没有变化,是我SQL语句写错了吗?
下面试代码。
主form传递dataset来当前页面。
最好留个联系方式,谢谢!
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.Data.SqlClient;
namespace 数据库大作业
{
public partial class customer : Form
{
Form fs = new Form();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
public customer(Form f,SqlDataAdapter data)
{
InitializeComponent();
fs = f;
da = data;
this.dataGridView1.ContextMenuStrip = this.contextMenuStrip1;
}
string connection = "Data Source=AFOGP-705072142\\TEST;Initial Catalog=message;User ID=leslie;Password=123456";
private void re_Click(object sender, EventArgs e)
{
this.Close();
fs.Show();
}
private void customer_Load(object sender, EventArgs e)
{
da.Fill(ds,"customer");
this.dataGridView1.DataSource = ds.Tables[0];
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//大概是下面这一段出了问题
SqlConnection cn = new SqlConnection(connection);
try
{
cn.Open();
this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
string mystr1 = dataGridView1.Columns[e.ColumnIndex].HeaderText + dataGridView1.CurrentCell.Value.ToString();
string mystr2 = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
string updatasql = "update customer set " + mystr1 + "where 身份证号=" + mystr2;
SqlCommand mycom = new SqlCommand(updatasql, cn);//修改数据库,mycon是数据库连接的对象
mycom.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cn.Close();
}
} 展开
下面试代码。
主form传递dataset来当前页面。
最好留个联系方式,谢谢!
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.Data.SqlClient;
namespace 数据库大作业
{
public partial class customer : Form
{
Form fs = new Form();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
public customer(Form f,SqlDataAdapter data)
{
InitializeComponent();
fs = f;
da = data;
this.dataGridView1.ContextMenuStrip = this.contextMenuStrip1;
}
string connection = "Data Source=AFOGP-705072142\\TEST;Initial Catalog=message;User ID=leslie;Password=123456";
private void re_Click(object sender, EventArgs e)
{
this.Close();
fs.Show();
}
private void customer_Load(object sender, EventArgs e)
{
da.Fill(ds,"customer");
this.dataGridView1.DataSource = ds.Tables[0];
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//大概是下面这一段出了问题
SqlConnection cn = new SqlConnection(connection);
try
{
cn.Open();
this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
string mystr1 = dataGridView1.Columns[e.ColumnIndex].HeaderText + dataGridView1.CurrentCell.Value.ToString();
string mystr2 = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
string updatasql = "update customer set " + mystr1 + "where 身份证号=" + mystr2;
SqlCommand mycom = new SqlCommand(updatasql, cn);//修改数据库,mycon是数据库连接的对象
mycom.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cn.Close();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询