c#net向sql操作添加更新删除数据.docx
《c#net向sql操作添加更新删除数据.docx》由会员分享,可在线阅读,更多相关《c#net向sql操作添加更新删除数据.docx(12页珍藏版)》请在冰豆网上搜索。
c#net向sql操作添加更新删除数据
c#向sql添加、更新、删除数据的原代码(winform)
好用
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespace学生成绩管理系统
{
publicpartialclassstudentluru:
Form
{
//CurrencyManagercmAmend;
//SqlConnectionsqlConnection1=newSqlConnection();
publicstudentluru()
{
InitializeComponent();
}
privatevoidstudentluru_Load(objectsender,EventArgse)
{
}
//录入
privatevoidbutton5_Click(objectsender,EventArgse)
{
stringsno=textBox1.Text;
stringsname=textBox2.Text;
stringssex=textBox3.Text;
stringsage=textBox4.Text;
stringsclass=textBox5.Text;
stringsdept=textBox6.Text;
stringsaddress=textBox7.Text;
stringsphone=textBox8.Text;
stringsqq=textBox9.Text;
if(textBox1.Text==""||textBox2.Text==""||textBox5.Text=="")
{
if(textBox1.Text=="")
{
MessageBox.Show("学号不能为空","警告提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
textBox1.Focus();
return;
}
if(textBox2.Text=="")
{
MessageBox.Show("姓名不能为空","警告提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
textBox2.Focus();
return;
}
if(textBox5.Text=="")
{
MessageBox.Show("班级不能为空","警告提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
textBox5.Focus();
return;
}
}
else
{
stringconnstr="server=IT32;uid=sa;pwd='sa';database=sc;";
try
{
SqlConnectionconn=newSqlConnection(connstr);
conn.Open();
stringsqlinsert="insertintostudentinfovalues('"+sno+"','"+sname+"','"+ssex+"','"+sage+"','"+sclass+"','"+sdept+"','"+saddress+"','"+sphone+"','"+sqq+"')";
SqlCommandsc=newSqlCommand(sqlinsert,conn);
sc.ExecuteNonQuery();
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
textBox4.Text="";
textBox5.Text="";
textBox6.Text="";
textBox7.Text="";
textBox8.Text="";
textBox9.Text="";
MessageBox.Show("数据已经添加成功","温馨提示");
conn.Close();
}
catch(Exceptionex)
{
MessageBox.Show(ex.ToString());
}
}
}
privatevoidbutton8_Click(objectsender,EventArgse)
{
this.Dispose();
}
}
}
这个是查询界面的代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespace学生成绩管理系统
{
publicpartialclassstudentchaxun:
Form
{
DataTabledt=newDataTable();
stringper;
stringsno;
CurrencyManagercmAmend;
publicstudentchaxun()
{
InitializeComponent();
}
publicstudentchaxun(stringk,strings)
{
InitializeComponent();
per=k;
sno=s;
}
privatevoidstudentchaxun_Load(objectsender,EventArgse)
{
//TODO:
这行代码将数据加载到表“studentDataSet.studentinfo”中。
您可以根据需要移动或移除它。
//this.studentinfoTableAdapter.Fill(this.studentDataSet.studentinfo);
//数据绑定
if(per=="超级用户")
{
stringsqlsel="select*fromstudentinfo";
DataTabledt=bangding(sqlsel);
cmAmend=(CurrencyManager)BindingContext[dt];
this.dataGridView1.DataSource=dt;
boBox1.DataSource=dt;
boBox1.DisplayMember="sno";
boBox2.DataSource=dt;
boBox2.DisplayMember="sname";
}
if(per=="普通用户")
{
stringsqlsel="select*fromstudentinfowheresno='"+sno+"'";
DataTabledt=bangding(sqlsel);
cmAmend=(CurrencyManager)BindingContext[dt];
this.dataGridView1.DataSource=dt;
boBox1.DataSource=dt;
boBox1.DisplayMember="sno";
boBox2.Enabled=false;
this.radioButton2.Enabled=false;
//this.toolStripButton1.Enabled=false;
//this.toolStripButton2.Enabled=false;
//this.toolStripButton3.Enabled=false;
//this.toolStripButton4.Enabled=false;
//boBox2.DataSource=dt;
//boBox2.DisplayMember="sname";
}
}
DataTablebangding(stringsqlsel)
{
stringconnstr="server=.;uid=sa;pwd=sa;database=sc";
using(SqlConnectionconn=newSqlConnection(connstr))
{
conn.Open();
DataSetds=newDataSet();
SqlDataAdapterda=newSqlDataAdapter(sqlsel,conn);
da.Fill(ds,"coust");
DataTabledt=ds.Tables["coust"];
conn.Close();
returndt;
}
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
if(this.radioButton1.Checked==true)
{
stringxuehao=comboBox1.Text;
stringsqlsel="select*fromstudentinfowheresno='"+xuehao+"'";
this.dataGridView1.DataSource=bangding(sqlsel);
}
else
{
stringxingming=comboBox2.Text;
stringsqlsel="select*fromstudentinfowheresname='"+xingming+"'";
this.dataGridView1.DataSource=bangding(sqlsel);
}
}
privatevoidradioButton1_CheckedChanged(objectsender,EventArgse)
{
if(this.radioButton1.Checked==true)
{
boBox1.Enabled=true;
boBox2.Enabled=false;
}
else
{
boBox1.Enabled=false;
boBox2.Enabled=true;
}
}
privatevoidcheckstate(intpos)
{
if(pos==0)
{
toolStripButton1.Enabled=false;
toolStripButton2.Enabled=false;
toolStripButton3.Enabled=true;
toolStripButton4.Enabled=true;
}
else
{
toolStripButton1.Enabled=true;
toolStripButton2.Enabled=true;
toolStripButton3.Enabled=true;
toolStripButton4.Enabled=true;
}
}
//首记录
privatevoidtoolStripButton1_Click(objectsender,EventArgse)
{
cmAmend.Position=0;//设置为0.回到首记录
this.dataGridView1.Select();
checkstate(cmAmend.Position);
}
//上一条
privatevoidtoolStripButton2_Click(objectsender,EventArgse)
{
cmAmend.Position--;
this.dataGridView1.Select();
checkstate(cmAmend.Position);
}
//下一条
privatevoidtoolStripButton3_Click(objectsender,EventArgse)
{
cmAmend.Position++;
this.dataGridView1.Select();
checkstate(cmAmend.Position);
}
//末记录
privatevoidtoolStripButton4_Click(objectsender,EventArgse)
{
cmAmend.Position=cmAmend.Count-1;
this.dataGridView1.Select();
checkstate(cmAmend.Position);
}
}
}
删除的代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespace学生成绩管理系统
{
publicpartialclassstudentshanchu:
Form
{
CurrencyManagercmAmend;
publicstudentshanchu()
{
InitializeComponent();
}
privatevoidstudentshanchu_Load(objectsender,EventArgse)
{
//TODO:
这行代码将数据加载到表“studentDataSet.studentinfo”中。
您可以根据需要移动或移除它。
//this.studentinfoTableAdapter.Fill(this.studentDataSet.studentinfo);
//数据绑定
stringsqlsel="select*fromstudentinfo";
DataTabledt=bangding(sqlsel);
cmAmend=(CurrencyManager)BindingContext[dt];
this.dataGridView1.DataSource=dt;
this.textBox1.DataBindings.Add("text",dt,"sno");
this.textBox2.DataBindings.Add("text",dt,"sname");
this.textBox3.DataBindings.Add("text",dt,"ssex");
this.textBox4.DataBindings.Add("text",dt,"sage");
this.textBox5.DataBindings.Add("text",dt,"sclass");
this.textBox6.DataBindings.Add("text",dt,"sdept");
this.textBox7.DataBindings.Add("text",dt,"saddress");
this.textBox8.DataBindings.Add("text",dt,"sphone");
this.textBox9.DataBindings.Add("text",dt,"sqq");
}
DataTablebangding(stringsqlsel)
{
stringconnstr="server=.;uid=sa;pwd=sa;database=sc";
using(SqlConnectionconn=newSqlConnection(connstr))
{
conn.Open();
DataSetds=newDataSet();
SqlDataAdapterda=newSqlDataAdapter(sqlsel,conn);
da.Fill(ds,"coust");
DataTabledt=ds.Tables["coust"];
conn.Close();
returndt;
}
}
privatevoidcheckBox1_CheckedChanged(objectsender,EventArgse)
{
if(checkBox1.Checked==true)
{this.Height=450;}
else
{this.Height=250;}
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
if(MessageBox.Show("你确定要删除该记录吗","询问",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
{
intpos=this.dataGridView1.CurrentCell.RowIndex;//获取该行
stringconnstr="server=.;uid=sa;pwd=sa;database=sc";
using(SqlConnectionconn=newSqlConnection(connstr))
{
conn.Open();
DataSetds=newDataSet();
stringsqlset="select*fromstudentinfo";
//数据集
SqlDataAdapterda=newSqlDataAdapter(sqlset,conn);
da.Fill(ds,"coust");
DataTabledt=ds.Tables["coust"];
SqlCommandBuildercb=newSqlCommandBuilder(da);
dt.Rows[pos].Delete();
da.Update(ds,"coust");
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
textBox4.Text="";
textBox5.Text="";
textBox6.Text="";
textBox7.Text="";
textBox8.Text="";
textBox9.Text="";
this.dataGridView1.DataSource=bangding(sqlset);
MessageBox.Show("恭喜你已成功删除","温馨提示");
conn.Close();
}
}
}
}
}
修改的代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespace学生成绩管理系统
{
publicpartialclassstudentxiugai:
Form
{
stringconnstr="server=.;uid=sa;pwd=sa;database=sc";
stringper;
stringsno;
CurrencyManagercmAmend;
publicstudentxiugai()
{
InitializeComponent();
}
publicstudentxiugai(stringk,strings)
{
InitializeComponent();
per=k;
sno=s;
}
privatevoidcheckBox1_CheckedChanged(objectsender,EventArgse)
{
if(checkBox1.Checked==true)
{//this.Height=450;
comboBox1.Enabled=false;
textBox1.Enabled=false;
}
else
{//this.Height=250;
comboBox1.Enabled=true;
textBox1.Enabled=true;
}
}
privatevoidstudentxiugai_Load(objectsender,EventArgse)
{
//数据绑定