C#常用增删改查源码.docx

上传人:b****2 文档编号:1762384 上传时间:2023-05-01 格式:DOCX 页数:20 大小:17KB
下载 相关 举报
C#常用增删改查源码.docx_第1页
第1页 / 共20页
C#常用增删改查源码.docx_第2页
第2页 / 共20页
C#常用增删改查源码.docx_第3页
第3页 / 共20页
C#常用增删改查源码.docx_第4页
第4页 / 共20页
C#常用增删改查源码.docx_第5页
第5页 / 共20页
C#常用增删改查源码.docx_第6页
第6页 / 共20页
C#常用增删改查源码.docx_第7页
第7页 / 共20页
C#常用增删改查源码.docx_第8页
第8页 / 共20页
C#常用增删改查源码.docx_第9页
第9页 / 共20页
C#常用增删改查源码.docx_第10页
第10页 / 共20页
C#常用增删改查源码.docx_第11页
第11页 / 共20页
C#常用增删改查源码.docx_第12页
第12页 / 共20页
C#常用增删改查源码.docx_第13页
第13页 / 共20页
C#常用增删改查源码.docx_第14页
第14页 / 共20页
C#常用增删改查源码.docx_第15页
第15页 / 共20页
C#常用增删改查源码.docx_第16页
第16页 / 共20页
C#常用增删改查源码.docx_第17页
第17页 / 共20页
C#常用增删改查源码.docx_第18页
第18页 / 共20页
C#常用增删改查源码.docx_第19页
第19页 / 共20页
C#常用增删改查源码.docx_第20页
第20页 / 共20页
亲,该文档总共20页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C#常用增删改查源码.docx

《C#常用增删改查源码.docx》由会员分享,可在线阅读,更多相关《C#常用增删改查源码.docx(20页珍藏版)》请在冰点文库上搜索。

C#常用增删改查源码.docx

C#常用增删改查源码

这个是添加的代码

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)

{

//数据绑定

if(per=="超级用户")

{

stringsqlsel="select*fromstudentinfoorderbysnoasc";

DataTabledt=bangding(sqlsel);

cmAmend=(CurrencyMana

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2