求相助 看看错在哪?删除 更新不好用
求帮助 看看错在哪?删除 更新不好用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DataSource
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataSet ds = new DataSet();
private SqlConnection conn = null;
private SqlDataAdapter da = null;
private const string DRIVER = "server=.;database=table1;uid=sa;pwd=qwe123!";
private const string sql_select = "select * from 日程";
private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection(DRIVER);
da = new SqlDataAdapter(sql_select, conn);
da.Fill(ds, "table");
this.dataGridView1.DataSource = ds.Tables["table"].DefaultView;
}
private bool BtnInsert()
{
da.InsertCommand = conn.CreateCommand();
da.InsertCommand.CommandText = "insert into 日程 values(@时间,@事件)";
da.InsertCommand.Parameters.Add("@时间", SqlDbType.NVarChar, 50, "时间");
da.InsertCommand.Parameters.Add("@事件", SqlDbType.NVarChar, 50, "事件");
int count = da.Update(ds);
bool result = count > 0 ? true : false;
return result;
}
private void button1_Click(object sender, EventArgs e)
{
if (this.BtnInsert())
{
MessageBox.Show("添加成功!");
}
else
{
MessageBox.Show("添加失败!");
}
}
private bool BtnDelect()
{
SqlParameter sp = new SqlParameter();
da.DeleteCommand = conn.CreateCommand();
da.DeleteCommand.CommandText = "delete 日程 where 时间=@时间";
sp = da.DeleteCommand.Parameters.Add("@时间", SqlDbType.NVarChar, 50, "事件");
sp.SourceVersion = DataRowVersion.Original;
ds.Tables["table"].Rows[this.dataGridView1.CurrentRow.Index].Delete();
int count = da.Update(ds);
bool result = count > 0 ? true : false;
return result;
}
private void button2_Click(object sender, EventArgs e)
{
if (this.BtnDelect())
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DataSource
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataSet ds = new DataSet();
private SqlConnection conn = null;
private SqlDataAdapter da = null;
private const string DRIVER = "server=.;database=table1;uid=sa;pwd=qwe123!";
private const string sql_select = "select * from 日程";
private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection(DRIVER);
da = new SqlDataAdapter(sql_select, conn);
da.Fill(ds, "table");
this.dataGridView1.DataSource = ds.Tables["table"].DefaultView;
}
private bool BtnInsert()
{
da.InsertCommand = conn.CreateCommand();
da.InsertCommand.CommandText = "insert into 日程 values(@时间,@事件)";
da.InsertCommand.Parameters.Add("@时间", SqlDbType.NVarChar, 50, "时间");
da.InsertCommand.Parameters.Add("@事件", SqlDbType.NVarChar, 50, "事件");
int count = da.Update(ds);
bool result = count > 0 ? true : false;
return result;
}
private void button1_Click(object sender, EventArgs e)
{
if (this.BtnInsert())
{
MessageBox.Show("添加成功!");
}
else
{
MessageBox.Show("添加失败!");
}
}
private bool BtnDelect()
{
SqlParameter sp = new SqlParameter();
da.DeleteCommand = conn.CreateCommand();
da.DeleteCommand.CommandText = "delete 日程 where 时间=@时间";
sp = da.DeleteCommand.Parameters.Add("@时间", SqlDbType.NVarChar, 50, "事件");
sp.SourceVersion = DataRowVersion.Original;
ds.Tables["table"].Rows[this.dataGridView1.CurrentRow.Index].Delete();
int count = da.Update(ds);
bool result = count > 0 ? true : false;
return result;
}
private void button2_Click(object sender, EventArgs e)
{
if (this.BtnDelect())