C#winform 程序,求高手帮我挑错!为什么我的int a=da.Update(ds, "login");中a的值不变(为默认值0)
也就是说我点击删除按钮时只能删除dataGridView中的数据,但是不更新到数据库中,为什么?以下是代码这是SqlHelp类:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace 旅游系统
{
public static class SqlHelp
{
private static SqlCommand cmd;
private static SqlConnection conn;
private static SqlDataAdapter sda;
private static DataSet ds;
public static SqlConnection getConn()
{
//返回数据库连接字符串
string constr = "Data Source=(local);Initial Catalog=tour;Integrated Security=true";
conn = new SqlConnection(constr);
return conn;
}
public static int ExcuteCmd(string sqlCmd)
{
int a = 0;
//执行一班的SQL语句(select,insert,delete,update)
conn = SqlHelp.getConn();
conn.Open();
cmd = new SqlCommand(sqlCmd, conn);//两个参数含义:要执行的SQL语句,连接对象
a= cmd.ExecuteNonQuery();//返回受影响的行
cmd.Dispose();
conn.Close();
return a;
}
public static DataSet getDataSet(string sqlCmd, string tableName)
{
//返回内存中一个数据集
conn = SqlHelp.getConn();
conn.Open();
sda = new SqlDataAdapter(sqlCmd, conn);//两个参数含义:Sql查询语句,所用到的数据库连接字符串
ds = new DataSet();
sda.Fill(ds, tableName);
conn.Close();
return ds;
}
public static SqlDataAdapter getSqlDataAdapter()
{
return sda;
}
public static DataTable getDataTable(string sqlCmd, string tableName)
{
conn = getConn();
conn.Open();
cmd = new SqlCommand(sqlCmd, conn);
sda = new SqlDataAdapter(cmd);
ds = new DataSet();
sda.Fill(ds, tableName);
DataTable dt = ds.Tables[tableName];
return dt;
}
public static SqlDataReader GetDataReader(string sqlStr)
{
conn = SqlHelp.getConn();
cmd = new SqlCommand(sqlStr, conn);
conn.Open();
SqlDataReader myReader=cmd.ExecuteReader (CommandBehavior.CloseConnection );//在执行该命令时,如果关闭关联的Datareader对象,则关联的Connection对象也将关闭
return myReader;
}
}
}
这是登录用户管理界面代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace 旅游系统
{
public partial class manager : Form
{
public manager()
{
InitializeComponent();
}
public static DataSet ds;
public static SqlCommandBuilder cb;
public static SqlDataAdapter da;
public void rename()
{
dataGridView1.Columns[0].HeaderText = "用户名";
dataGridView1.Columns[1].HeaderText = "密码";
dataGridView1.Columns[2].HeaderText = "用户类型";
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill ;
dataGridView1.AutoSizeRowsMode=DataGridViewAutoSizeRowsMode.AllCells ;
}
private void manager_Load(object sender, EventArgs e)
{
string sqlstr = "select * from log ";
ds = SqlHelp.getDataSet(sqlstr, "login");
da = SqlHelp.getSqlDataAdapter();
cb = new SqlCommandBuilder(da );
dataGridView1.DataSource = SqlHelp.getDataTable(sqlstr, "login");
rename();
}
private void button_update_Click(object sender, EventArgs e)
{
}
private void button_del_Click(object sender, EventArgs e)
{
int a = 0;
foreach (DataGridViewRow dgv in dataGridView1.SelectedRows)
{
dataGridView1.Rows.Remove(dgv);
a=da.Update(ds, "login");
}
}
}
}
--------------------编程问答-------------------- dataGridView1.Rows.Remove(dgv);
a=da.Update(ds, "login");
你这个只是更新的dataGridView1,没有更新数据库
你也没有执行ExcuteCmd的update啊 --------------------编程问答-------------------- 那我代码应该怎么写 --------------------编程问答-------------------- 这个地方要么 你用DataGridView绑定数源 那种方式可以改变后直接更新到数据库, 如果是在代码里手动绑定的话,就应该获取到你要更新这一列的主键或者是别的列 然后根据这个列 产生相应的SQL语句 执行更新操作...
补充:.NET技术 , C#