是不是直接用command? link = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=F:/website2/DataBase/users.mdb"); link.Open(); cmd = new OleDbCommand("delete from users", link); 这样吗?
删除 、修改 、查询 都在这个类里边using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using Microsoft.Office.Core; using Excel = Microsoft.Office.Interop.Excel;namespace FileInput { public partial class PhotoListForm : Form { private DBHelper dbHelper = new DBHelper(); private Photo photo = new Photo(); private DataSet dataSet = new DataSet(); //声明并初始化DataSet private SqlDataAdapter dataAdapter; //声明DataAdapter private int currentPage = 1; //当前页为第一页 private int pageSize = 15; //每页显示15条记录 /// <summary> /// 查询所有照片档案 /// </summary> public void GetList() { this.lblPageCount.Text = currentPage + "/" + this.GetPageCount(); //查询SQL语句 string sql = "Select p_photoNo AS 档号,p_dutyer AS 责任者,p_title AS 题名,p_writNo AS 文号,p_time AS 创建日期,p_pageNo AS 页数 from photo order by p_time desc"; //初始化DataAdapter dataAdapter = new SqlDataAdapter(sql, dbHelper.GetConnection()); int startRow = (currentPage - 1) * pageSize; //填充DataSet dataAdapter.Fill(dataSet, startRow, pageSize, "photo"); //指定DGV的数据源 dgvPhoto.DataSource = dataSet.Tables["photo"]; } /// <summary> /// 多条件查询 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSearch_Click(object sender, EventArgs e) { currentPage = 1; //instruments = new Instruments(); photo.P_photoNo = this.txtPhotoNo.Text; photo.P_title = this.txtTitle.Text; photo.P_dutyer = this.txtDutyer.Text; photo.P_writNo = this.txtWritNo.Text; int year = 0; if (Year.years != 0) { year = Year.years; } dataSet.Tables["photo"].Clear(); //查询SQL语句 string sql = "Select p_photoNo AS 档号,p_dutyer AS 责任者,p_title AS 题名,p_writNo AS 文号,p_time AS 创建日期,p_pageNo AS 页数 from photo where 1=1"; if (!photo.P_photoNo.Equals("")) { sql += " and p_photoNo like '%" + photo.P_photoNo + "%'"; } if (!photo.P_title.Equals("")) { sql += " and p_title like '%" + photo.P_title + "%'"; } if (!photo.P_dutyer.Equals("")) { sql += "and p_dutyer like '%" + photo.P_dutyer + "%'"; } else if (!photo.P_writNo.Equals("")) { sql += "and p_writNo like '%" + photo.P_writNo + "%'"; } else if (year != 0) { sql += " and p_year= " + year + " "; } sql += " order by p_year desc"; //初始化DataAdapter dataAdapter = new SqlDataAdapter(sql, dbHelper.GetConnection()); int startRow = (currentPage - 1) * pageSize; //填充DataSet dataAdapter.Fill(dataSet, startRow, pageSize, "photo"); //指定DGV的数据源 dgvPhoto.DataSource = dataSet.Tables["photo"]; this.GetPageCount(); this.GetCount(); this.btnPrev.Enabled = false; if (currentPage == this.GetPageCount() || this.GetPageCount() == 0) { this.btnNext.Enabled = false; } else { this.btnNext.Enabled = true; } this.lblPageCount.Text = currentPage + "/" + this.GetPageCount(); } /// <summary> /// 右键删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tsmiDelete_Click(object sender, EventArgs e) { //确保用户选择了一条档案信息才能执行删除操作 if (dgvPhoto.SelectedRows.Count == 0) { MessageBox.Show("请选中一条档案信息", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } DialogResult choice = MessageBox.Show("删除后不可恢复,确认要删除吗?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); //如果确定删除 if (choice == DialogResult.Yes) { int result = 0; try { string sql = string.Format("Delete From photo Where p_photoNo ='{0}'", dgvPhoto.SelectedRows[0].Cells[0].Value); //创建SQLCommand对象 SqlCommand command = new SqlCommand(sql, dbHelper.GetConnection()); dbHelper.GetConnection().Open(); result = (int)command.ExecuteNonQuery(); //执行删除操作 } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show("操作数据库出错,请稍后再试", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information); throw ex; } finally { dbHelper.close(); } if (result < 1) //删除失败 { MessageBox.Show("删除失败", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else //删除成功 { MessageBox.Show("删除成功", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information); dataSet.Tables["photo"].Clear(); this.GetList(); } } } /// <summary> /// 右键修改 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tsmiUpdate_Click(object sender, EventArgs e) { //确保用户选择了一条档案信息才能执行删除操作 if (dgvPhoto.SelectedRows.Count == 0) { MessageBox.Show("请选择要修改的档案", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { try { string sql = string.Format("Select * From photo Where p_photoNo ='{0}'", dgvPhoto.SelectedRows[0].Cells[0].Value); //创建SQLCommand对象 SqlCommand command = new SqlCommand(sql, dbHelper.GetConnection()); //打开数据库连接 dbHelper.GetConnection().Open(); //创建DataReader对象 SqlDataReader dataReader = command.ExecuteReader(); if (dataReader.Read()) { photo.P_photoNo = (string)dataReader[0]; photo.P_filmNo = (string)dataReader[1]; photo.P_anjuan = (string)dataReader[2]; photo.P_roomNo = (string)dataReader[3]; photo.P_dutyer = (string)dataReader[4]; photo.P_title = (string)dataReader[5]; photo.P_writNo = (string)dataReader[6]; photo.P_time = (string)dataReader[7]; photo.P_place = (string)dataReader[8]; photo.P_people = (string)dataReader[9]; photo.P_cause = (string)dataReader[10]; photo.P_background = (string)dataReader[11]; photo.P_photographer = (string)dataReader[12]; photo.P_secretLevel = (string)dataReader[13]; photo.P_saveTimer = (string)dataReader[14]; photo.P_pageNo = (int)dataReader[15]; photo.P_annex= (string)dataReader[16]; photo.P_note = (string)dataReader[17]; photo.P_year = (int)dataReader[18]; } dataReader.Close(); //关闭DataReader } catch (Exception) { throw; } finally { dbHelper.close(); //关闭数据库连接 } } PhotoUpdateForm photoUpdate = new PhotoUpdateForm(photo); photoUpdate.Show(); this.Visible = false; } } }
SqlCommand来操作
如果是绑定到DataGridView的话,在DataGridView处理完后用SqlDataAdapter来更新
link = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=F:/website2/DataBase/users.mdb");
link.Open();
cmd = new OleDbCommand("delete from users", link);
这样吗?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;namespace FileInput
{
public partial class PhotoListForm : Form
{
private DBHelper dbHelper = new DBHelper();
private Photo photo = new Photo();
private DataSet dataSet = new DataSet(); //声明并初始化DataSet
private SqlDataAdapter dataAdapter; //声明DataAdapter
private int currentPage = 1; //当前页为第一页
private int pageSize = 15; //每页显示15条记录
/// <summary>
/// 查询所有照片档案
/// </summary>
public void GetList()
{
this.lblPageCount.Text = currentPage + "/" + this.GetPageCount(); //查询SQL语句
string sql = "Select p_photoNo AS 档号,p_dutyer AS 责任者,p_title AS 题名,p_writNo AS 文号,p_time AS 创建日期,p_pageNo AS 页数 from photo order by p_time desc";
//初始化DataAdapter
dataAdapter = new SqlDataAdapter(sql, dbHelper.GetConnection()); int startRow = (currentPage - 1) * pageSize;
//填充DataSet
dataAdapter.Fill(dataSet, startRow, pageSize, "photo"); //指定DGV的数据源
dgvPhoto.DataSource = dataSet.Tables["photo"];
}
/// <summary>
/// 多条件查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSearch_Click(object sender, EventArgs e)
{
currentPage = 1; //instruments = new Instruments();
photo.P_photoNo = this.txtPhotoNo.Text;
photo.P_title = this.txtTitle.Text;
photo.P_dutyer = this.txtDutyer.Text;
photo.P_writNo = this.txtWritNo.Text; int year = 0;
if (Year.years != 0)
{
year = Year.years;
}
dataSet.Tables["photo"].Clear(); //查询SQL语句
string sql = "Select p_photoNo AS 档号,p_dutyer AS 责任者,p_title AS 题名,p_writNo AS 文号,p_time AS 创建日期,p_pageNo AS 页数 from photo where 1=1";
if (!photo.P_photoNo.Equals(""))
{
sql += " and p_photoNo like '%" + photo.P_photoNo + "%'";
}
if (!photo.P_title.Equals(""))
{
sql += " and p_title like '%" + photo.P_title + "%'";
}
if (!photo.P_dutyer.Equals(""))
{
sql += "and p_dutyer like '%" + photo.P_dutyer + "%'";
}
else if (!photo.P_writNo.Equals(""))
{
sql += "and p_writNo like '%" + photo.P_writNo + "%'";
}
else if (year != 0)
{
sql += " and p_year= " + year + " ";
}
sql += " order by p_year desc";
//初始化DataAdapter
dataAdapter = new SqlDataAdapter(sql, dbHelper.GetConnection()); int startRow = (currentPage - 1) * pageSize;
//填充DataSet
dataAdapter.Fill(dataSet, startRow, pageSize, "photo"); //指定DGV的数据源
dgvPhoto.DataSource = dataSet.Tables["photo"]; this.GetPageCount();
this.GetCount();
this.btnPrev.Enabled = false;
if (currentPage == this.GetPageCount() || this.GetPageCount() == 0)
{
this.btnNext.Enabled = false;
}
else
{
this.btnNext.Enabled = true;
}
this.lblPageCount.Text = currentPage + "/" + this.GetPageCount();
} /// <summary>
/// 右键删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsmiDelete_Click(object sender, EventArgs e)
{
//确保用户选择了一条档案信息才能执行删除操作
if (dgvPhoto.SelectedRows.Count == 0)
{
MessageBox.Show("请选中一条档案信息", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
DialogResult choice = MessageBox.Show("删除后不可恢复,确认要删除吗?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
//如果确定删除
if (choice == DialogResult.Yes)
{
int result = 0;
try
{
string sql = string.Format("Delete From photo Where p_photoNo ='{0}'", dgvPhoto.SelectedRows[0].Cells[0].Value);
//创建SQLCommand对象
SqlCommand command = new SqlCommand(sql, dbHelper.GetConnection());
dbHelper.GetConnection().Open();
result = (int)command.ExecuteNonQuery(); //执行删除操作
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show("操作数据库出错,请稍后再试", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
throw ex;
}
finally
{
dbHelper.close();
} if (result < 1) //删除失败
{
MessageBox.Show("删除失败", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else //删除成功
{
MessageBox.Show("删除成功", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
dataSet.Tables["photo"].Clear();
this.GetList();
}
}
} /// <summary>
/// 右键修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsmiUpdate_Click(object sender, EventArgs e)
{
//确保用户选择了一条档案信息才能执行删除操作
if (dgvPhoto.SelectedRows.Count == 0)
{
MessageBox.Show("请选择要修改的档案", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
try
{
string sql = string.Format("Select * From photo Where p_photoNo ='{0}'", dgvPhoto.SelectedRows[0].Cells[0].Value);
//创建SQLCommand对象
SqlCommand command = new SqlCommand(sql, dbHelper.GetConnection());
//打开数据库连接
dbHelper.GetConnection().Open();
//创建DataReader对象
SqlDataReader dataReader = command.ExecuteReader();
if (dataReader.Read())
{
photo.P_photoNo = (string)dataReader[0];
photo.P_filmNo = (string)dataReader[1];
photo.P_anjuan = (string)dataReader[2];
photo.P_roomNo = (string)dataReader[3];
photo.P_dutyer = (string)dataReader[4];
photo.P_title = (string)dataReader[5];
photo.P_writNo = (string)dataReader[6];
photo.P_time = (string)dataReader[7];
photo.P_place = (string)dataReader[8];
photo.P_people = (string)dataReader[9];
photo.P_cause = (string)dataReader[10];
photo.P_background = (string)dataReader[11];
photo.P_photographer = (string)dataReader[12];
photo.P_secretLevel = (string)dataReader[13];
photo.P_saveTimer = (string)dataReader[14];
photo.P_pageNo = (int)dataReader[15];
photo.P_annex= (string)dataReader[16];
photo.P_note = (string)dataReader[17];
photo.P_year = (int)dataReader[18];
}
dataReader.Close(); //关闭DataReader
}
catch (Exception)
{
throw;
}
finally
{
dbHelper.close(); //关闭数据库连接
}
}
PhotoUpdateForm photoUpdate = new PhotoUpdateForm(photo);
photoUpdate.Show();
this.Visible = false;
}
}
}
/// 增加档案信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSave_Click(object sender, EventArgs e)
{
instruments = new Instruments();
//获取要插入到数据库每个字段的值
instruments.In_id = this.txtIn_id.Text;
instruments.Anjuan = this.txtAnjuan.Text;
int recordNo = Convert.ToInt32(this.lblRecordNo.Text);
instruments.Type = this.txtType.Text;
instruments.RoomNo = this.txtRoomNo.Text;
instruments.Dutyer = this.txtDutyer.Text;
instruments.Title = this.txtTitle.Text;
instruments.WritNo = this.txtWritNo.Text;
instruments.Text = this.cboText.Text;
instruments.SecretLevel = this.cboSecretLevel.Text;
instruments.SaveTimer = this.cboSaveTimer.Text;
instruments.CreateTime =this.txtCreateTime.Text;
try
{
instruments.PageNo = Convert.ToInt32(this.txtPageNo.Text);
}
catch (Exception)
{
MessageBox.Show("页数只能为数字", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
instruments.Annex = this.txtAnnex.Text;
instruments.Note = this.txtNote.Text;
instruments.Zhutici = this.txtZhutici.Text;
instruments.Summary = this.txtSummary.Text;
instruments.Year = Convert.ToInt32(this.lblYear.Text); //构建插入的SQL语句
string sql = string.Format("Insert Into instrument(in_id,anjuan,type,roomNo,dutyer,title,writNo,text,secretLevel,saveTimer,createTime,pageNo,annex,note,zhutici,summary,year) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}',{11},'{12}','{13}','{14}','{15}',{16})", instruments.In_id, instruments.Anjuan, instruments.Type, instruments.RoomNo, instruments.Dutyer, instruments.Title, instruments.WritNo, instruments.Text, instruments.SecretLevel, instruments.SaveTimer, instruments.CreateTime, instruments.PageNo, instruments.Annex, instruments.Note, instruments.Zhutici, instruments.Summary, instruments.Year);
try
{
//创建Command对象
SqlCommand command = new SqlCommand(sql, dbHelper.GetConnection());
dbHelper.GetConnection().Open(); //打开数据库连接
int result = command.ExecuteNonQuery(); //执行命令
//根据操作结果给出提示信息
if (result != 1)
{
MessageBox.Show("保存失败!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("保存成功", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnReset_Click(sender, e);
}
}
catch (Exception ex)
{
MessageBox.Show("操作数据库出错,请稍后再试!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
Console.WriteLine(ex.Message);
//Application.Exit();
}
finally
{
dbHelper.close(); //关闭数据库连接
}
}
删:delete from tablename where id = ;改:update tablename set where id = ;查:select * from tablename
gridview中显示的字段怎么带超链接?
如题。gridview绑定数据库,可以显示数据。请问,这个数据怎么带超链接?
链接是hyperlink的url用id传的值
sqlcommand
这两个足以实现增删改