求一个c#.net2005向sql添加、更新、删除数据的原代码

作者&投稿:祁嘉 (若有异议请与网页底部的电邮联系)
C#+Sql数据库+管理员删除和添加一个用户源代码~

晕,链接数据库,写好sql字符串,执行就行了,还要源代码啊

string tempTableName = "ProductType";//数据库表名
String tempRowName = "row";//节点
String tempSavePath = "../Employees.xml";//保存地址

DataTable datatd = new DataTable();
using (SqlConnection sqlcon = new SqlConnection())
{
sqlcon.ConnectionString = "server=192.168.11.111;database=Insects;uid=sa;pwd=2275570";
using (SqlCommand sqlcomm = sqlcon.CreateCommand())
{
sqlcomm.CommandText = "select * from " + tempTableName;
SqlDataAdapter tempa = new SqlDataAdapter(sqlcomm);
tempa.Fill(datatd);
}
}

XElement tempDoc = new XElement(tempTableName);
for (int i = 0; i < datatd.Rows.Count; i++)
{
var tempElement = new XElement(tempRowName);
for (int j = 0; j < datatd.Columns.Count; j++)
{
tempElement.Add(new XElement(datatd.Columns[j].ColumnName, datatd.Rows[i][j] == DBNull.Value ? String.Empty : datatd.Rows[i][j]));
}
tempDoc.Add(tempElement);

}
tempDoc.Save(tempSavePath);

这个是添加的代码
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;

namespace 学生成绩管理系统
{
public partial class studentluru : Form
{
// CurrencyManager cmAmend;
// SqlConnection sqlConnection1 = new SqlConnection();

public studentluru()
{
InitializeComponent();
}

private void studentluru_Load(object sender, EventArgs e)
{

}

//录入
private void button5_Click(object sender, EventArgs e)
{
string sno = textBox1.Text;
string sname = textBox2.Text;
string ssex = textBox3.Text;
string sage = textBox4.Text;
string sclass = textBox5.Text;
string sdept = textBox6.Text;
string saddress = textBox7.Text;
string sphone = textBox8.Text;
string sqq = 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
{

string connstr = "server=IT32;uid=sa;pwd='sa';database=sc;";
try
{
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string sqlinsert = "insert into studentinfo values('" + sno + "','" + sname + "','" + ssex + "','" + sage + "','" + sclass + "','" + sdept + "','" + saddress + "','" + sphone + "','" + sqq + "')";
SqlCommand sc = new SqlCommand(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 (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}

private void button8_Click(object sender, EventArgs e)
{
this.Dispose();
}

}
}

这个是查询界面的代码:
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;

namespace 学生成绩管理系统
{
public partial class studentchaxun : Form
{

DataTable dt = new DataTable();
string per;
string sno;
CurrencyManager cmAmend;

public studentchaxun()
{
InitializeComponent();
}
public studentchaxun(string k, string s)
{
InitializeComponent();
per = k;
sno = s;
}

private void studentchaxun_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“studentDataSet.studentinfo”中。您可以根据需要移动或移除它。
//this.studentinfoTableAdapter.Fill(this.studentDataSet.studentinfo);
//数据绑定
if (per == "超级用户")
{
string sqlsel = "select * from studentinfo ";
DataTable dt = bangding(sqlsel);
cmAmend = (CurrencyManager)BindingContext[dt];
this.dataGridView1.DataSource = dt;
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "sno";
this.comboBox2.DataSource = dt;
this.comboBox2.DisplayMember = "sname";
}
if (per == "普通用户")
{
string sqlsel = "select * from studentinfo where sno='" + sno + "' ";
DataTable dt = bangding(sqlsel);
cmAmend = (CurrencyManager)BindingContext[dt];
this.dataGridView1.DataSource = dt;
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "sno";
this.comboBox2.Enabled = false;
this.radioButton2.Enabled = false;
//this.toolStripButton1.Enabled = false;
//this.toolStripButton2.Enabled = false;
//this.toolStripButton3.Enabled = false;
//this.toolStripButton4.Enabled = false;
//this.comboBox2.DataSource = dt;
//this.comboBox2.DisplayMember = "sname";
}
}

DataTable bangding(string sqlsel)
{
string connstr = "server=.;uid=sa;pwd=sa;database=sc";

using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn);
da.Fill(ds, "coust");
DataTable dt = ds.Tables["coust"];

conn.Close();
return dt;
}

}

private void button1_Click(object sender, EventArgs e)
{
if (this.radioButton1.Checked == true)
{
string xuehao = comboBox1.Text;
string sqlsel = "select * from studentinfo where sno='" + xuehao + "'";
this.dataGridView1.DataSource = bangding(sqlsel);
}
else
{
string xingming = comboBox2.Text;
string sqlsel = "select * from studentinfo where sname='" + xingming + "'";
this.dataGridView1.DataSource = bangding(sqlsel);
}
}

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton1.Checked == true)
{
this.comboBox1.Enabled = true;
this.comboBox2.Enabled = false;

}
else
{
this.comboBox1.Enabled = false;
this.comboBox2.Enabled = true;

}
}

private void checkstate(int pos)
{
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;
}
}
//首记录
private void toolStripButton1_Click(object sender, EventArgs e)
{
cmAmend.Position = 0;//设置为0.回到首记录
this.dataGridView1.Select();
checkstate(cmAmend.Position);
}
//上一条
private void toolStripButton2_Click(object sender, EventArgs e)
{
cmAmend.Position--;
this.dataGridView1.Select();

checkstate(cmAmend.Position);

}
//下一条
private void toolStripButton3_Click(object sender, EventArgs e)
{
cmAmend.Position++;
this.dataGridView1.Select();
checkstate(cmAmend.Position);

}
//末记录
private void toolStripButton4_Click(object sender, EventArgs e)
{
cmAmend.Position = cmAmend.Count - 1;
this.dataGridView1.Select();

checkstate(cmAmend.Position);
}

}
}
删除的代码:
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;

namespace 学生成绩管理系统
{
public partial class studentshanchu : Form
{
CurrencyManager cmAmend;

public studentshanchu()
{
InitializeComponent();
}

private void studentshanchu_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“studentDataSet.studentinfo”中。您可以根据需要移动或移除它。
//this.studentinfoTableAdapter.Fill(this.studentDataSet.studentinfo);
//数据绑定
string sqlsel = "select * from studentinfo ";
DataTable dt = 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");

}

DataTable bangding(string sqlsel)
{
string connstr = "server=.;uid=sa;pwd=sa;database=sc";
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn);
da.Fill(ds, "coust");
DataTable dt = ds.Tables["coust"];
conn.Close();
return dt;
}

}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{ this.Height = 450; }
else
{ this.Height = 250; }
}

private void button1_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你确定要删除该记录吗", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
int pos = this.dataGridView1.CurrentCell.RowIndex; //获取该行

string connstr = "server=.;uid=sa;pwd=sa;database=sc";
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
DataSet ds = new DataSet();
string sqlset = "select * from studentinfo";
//数据集
SqlDataAdapter da = new SqlDataAdapter(sqlset, conn);
da.Fill(ds, "coust");
DataTable dt = ds.Tables["coust"];
SqlCommandBuilder cb = new SqlCommandBuilder(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();
}
}
}
}
}

修改的代码:
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;

namespace 学生成绩管理系统
{
public partial class studentxiugai : Form
{
string connstr = "server=.;uid=sa;pwd=sa;database=sc";
string per;
string sno;
CurrencyManager cmAmend;

public studentxiugai()
{
InitializeComponent();
}

public studentxiugai(string k,string s)
{
InitializeComponent();
per = k;
sno = s;
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{ //this.Height = 450;
comboBox1.Enabled = false;
textBox1.Enabled = false;
}
else
{ //this.Height = 250;
comboBox1.Enabled = true;
textBox1.Enabled = true;
}
}

private void studentxiugai_Load(object sender, EventArgs e)
{
//数据绑定
if (per == "超级用户")
{
string sqlsel = "select * from studentinfo order by sno asc ";
DataTable dt = bangding(sqlsel);
cmAmend = (CurrencyManager)BindingContext[dt];

this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "sno";
this.textBox1.DataBindings.Add("text", dt, "sname");
this.textBox2.DataBindings.Add("text", dt, "ssex");
this.textBox3.DataBindings.Add("text", dt, "sage");
this.textBox4.DataBindings.Add("text", dt, "sclass");
this.textBox5.DataBindings.Add("text", dt, "sdept");
this.textBox6.DataBindings.Add("text", dt, "saddress");
this.textBox7.DataBindings.Add("text", dt, "sphone");
this.textBox8.DataBindings.Add("text", dt, "sqq");
}
if (per == "普通用户")
{
string sqlsel = "select *from studentinfo where sno='" + sno + "'";
DataTable dt = bangding(sqlsel);
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "sno";
this.textBox1.DataBindings.Add("text", dt, "sname");
this.textBox2.DataBindings.Add("text", dt, "ssex");
this.textBox3.DataBindings.Add("text", dt, "sage");
this.textBox4.DataBindings.Add("text", dt, "sclass");
this.textBox5.DataBindings.Add("text", dt, "sdept");
this.textBox6.DataBindings.Add("text", dt, "saddress");
this.textBox7.DataBindings.Add("text", dt, "sphone");
this.textBox8.DataBindings.Add("text", dt, "sqq");
}
}

DataTable bangding(string sqlsel)
{
// string connstr = "server=.;uid=sa;pwd=sa;database=sc";
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn);
da.Fill(ds, "coust");
DataTable dt = ds.Tables["coust"];
conn.Close();
return dt;
}

}

private void button1_Click(object sender, EventArgs e)

{
if (checkBox1.Checked == true)
{
string sno = comboBox1.Text;
string sname = textBox1.Text;
string sex = textBox2.Text;
string age = textBox3.Text;
string sclass = textBox4.Text;
string sdept = textBox5.Text;
string saddress = textBox6.Text;
string sphone = textBox7.Text;
string sqq = textBox8.Text;
if (textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "")
{
if (textBox2.Text == "")
{

MessageBox.Show("性别不能为空", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
textBox2.Focus();
return;
}

if (textBox3.Text == "")
{

MessageBox.Show("年龄不能为空", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
textBox3.Focus();
return;
}

if (textBox4.Text == "")
{

MessageBox.Show("班级不能为空", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
textBox4.Focus();
return;
}
if (textBox5.Text == "")
{

MessageBox.Show("系别不能为空", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
textBox5.Focus();
return;
}
}
else
{

string connstr = "server=IT32;uid=sa;pwd='sa';database=sc;";
try
{
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string sqlupdate = " update studentinfo set ssex='" + sex + "',sage='" + age + "',sclass='" + sclass + "',sdept='" + sdept + "',saddress='" + saddress + "',sphone='" + sphone + "',sqq='" + sqq + "' where sno='" + sno + "'";
SqlCommand sc = new SqlCommand(sqlupdate, conn);
sc.ExecuteNonQuery();
MessageBox.Show("数据已经修改成功", "温馨提示");
conn.Close();

}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
else
{
MessageBox.Show("对不起,你还没有选中'我要修改',所以您无法修改","温馨提示");
}
}
}
}

更新的代码太简单了,只需要重新在数据库里面查询一次并把它取出来就行了,这里就不写了,累死我了,这次应该看懂了吧?

http://hi.baidu.com/gamevip/blog/item/9c2fb245046dc725cefca3d7.html
http://hi.baidu.com/gamevip/blog/item/c43599822ef1fbbc6d8119fc.html
http://hi.baidu.com/gamevip/blog/item/c5fa96130dd8fd836438dbf1.html
http://hi.baidu.com/gamevip/blog/item/9ecfbd99dbcd750b6f068cdf.html

因该很容易看懂.

有公用的数据库操作函数看月下就好了啊


一个正写的“C”,一个反写的“C”,这是什么牌子?
一个正写的“C”,一个反写的“C”,这是香奈儿牌子。香奈儿(Chanel)是一个法国奢侈品品牌,创始人是Coco Chanel(原名是Gabrielle Bonheur Chanel ,中文名是加布里埃·可可·香奈儿),该品牌于1910年在法国巴黎创立。该品牌产品种类繁多,有服装、珠宝饰品及其配件、化妆品、护肤品、香水等。该品牌的...

圆圈里一个“C”是什么意思?
圆圈里一个“C”是版权标记。中国传统使用的版权标记为 “版权所有” 字样。版权标记通常还须伴有关于作品版权情况的说明文字。例如《世界版权公约》规定,版权标记由版权符号©即英文copyright(版权)一词的首字母外加一个圆圈,版权所有者的姓名以及作品首次出版或注册年份三部分组成。版权标记须刊载在...

c语言模块化程序设计是将一个大的任务分解成若干个小任务吗?
选A。一个C程序由一个主函数和若干个其他函数组成。若干个的意思就是可以有0个及以上个。c语言的程序模块称为函数。模块化程序设计是将一个大的任务分解成若干个小任务,再将小任务分解成更小的任务,直到每一个任务都只完成一个独立功能。这样的每个任务都叫做模块,c语言中模块是用函数来实现。

一个C一斜杠的符号怎么打
第一种,直接搜狗拼音打“ 美分”后面就出现了。第二种,shift+ctrl+z 选择“数学\/单位“,找一下就有这个¢符号。

一个正写的C,一个倒写的C,相对着的,是什么牌子?不是chanel。
一个正写的C,一个倒写的C如果不是香奈儿,那么应该是美国滑板品牌 DC Shoes DC Shoes简介: DC SHOES是高性能滑板鞋方面的领先公司,其产品包括男女休闲服装、滑雪鞋和外衣、各种配饰以及儿童和幼儿用滑板鞋。作为其策略的基石之一,DC组建了一个由专业滑板、滑雪、冲浪、BMX、摩托车越野运动员组成的世界一流的团队,...

一个圆圈里面一个C是什么意思?
一个圆圈,里面有个C的符号是版权符号©。直接输入的,可以用搜索输入法,打版权全拼banquan就会显示出来了。另外在Word中打版权符号©的方法如下:1、点击菜单栏插入;2、在插入的工具栏里点击倒数第二个符号的下拉倒三角;3、然后点击其他符号;4、进入符号窗口,点击符号,字体选择“(普通...

325. 一个 C类地址:192.168.5.0 ,进行子网规划,要求每个子网有 10台主...
因为IP地址为C类地址,又因为2^3=8<10<2^4=16,所以子网掩码长度应为3*8+4=28即子网掩码为255.255.255.240。网络:192.168.5.0。掩码:255.255.255.240\/28。划分:16个子网,每个子网16个地址。子网掩码是一个32位地址,用于屏蔽IP地址的一部分以区别网络标识和主机标识,并说明该IP地址...

1C的电荷量相当于多少个质子所带的电荷量。这怎么算
元电荷是指最小的电荷量,等于一个质子带的电荷量,带电荷量为:e=1.6×10^-19C。元电荷的电量是e=1.60×10^-19C,所以1C÷(1.60×10^-19C)=6.25×10^18个。假若两个物质都带有正电或都带有负电,则称这两个物质“同电性”,否则称这两个物质“异电性”。两个同电性物质会相互...

圆圈里面一个c是什么意思
这是版权已注册的意思。圈里有R是商标注册的意思。你注意看许多网站最下方都有这个圈加C标志,那是表明人家已作版面著作权登记,请勿模仿、抄袭。

如何把一个复杂的C语言源代码分成几个文件,然后在dev c++上进行多文件...
假设我们要更改的源代码如下:我们的任务是把xx写成一个头文件,将ff写成一个头文件。头文件基本格式:ifndef _xx_h_define _xx_h_\/\/do something endif 第一步:首先将每个头文件写好,如下:注意在源文件使用头文件时要用“”而不能用<>,即:include "ff.h" 而不是 #include <ff.h> 第...

沧浪区15750193347: .net c# 向Sql Server 2005中插入一行数据 -
兴信启脾: insert into TOTABLE (a,b) vaelus select 字段1 as a ,字段2 as b from FROMTABLE WHERE id=1

沧浪区15750193347: VS2005中,使用C#语言连接SQL数据库,然后实现插入数据的方法. -
兴信启脾: try<br> {<br> String commandText="select count(*) from users";<br> SqlConnection conn = new SqlConnection();<br> conn.ConnectionString = "Data Source=(local);Initial Catalog=classmate;User Id=sa;Password=sa";<br><br> ...

沧浪区15750193347: c#.net数据库连接sql2005 -
兴信启脾: //连接数据库 public SqlConnection ConnectionDataBase() { SqlConnection con = new SqlConnection("server=;uid=用户名;pwd= 用户密码;database=数据库名字 "); return con; } //插入public bool CreateAddressBook(Data data) { bool c...

沧浪区15750193347: Visual studio.net 2005 怎么用c#连接sql2005啊
兴信启脾: string name ='数据库的内容'; sqlconnection conn = new sqlconnection(name) 数据库内容可以通过工具,连接数据库,输入数据库的名称,选择数据库,确定,右面有具体的属性, 把属性复制到 '数据库的内容'就可以了,记住在前面加 '数据库'的前面加@ ,否则会报错

沧浪区15750193347: 求一个.net C#连接数据库例子! -
兴信启脾: 这个吧!只有查,增删改都差不多!就不多说了!SqlConnection con = new SqlConnection(); con.ConnectionString = @"server=***;database=***; uid=sa; pwd=**********"; con.Open(); string strSql = "select * from A"; SqlCommand cmd= ...

沧浪区15750193347: 请问现在.net 2005 c#可以使用嵌入式SQL吗? -
兴信启脾: 这个: @: 来解释下,这是数据库连接的驱动,每个参数的意思如下.dbname : 数据库名称localhost : 程序连接要用到数...

沧浪区15750193347: C#远程连接Sqlserver2005数据库并建表 -
兴信启脾: c# 是通过ADO.NET连接数据库的,直接改连接字符串就可以实现远程连接了,但前提是你能访问到那台电脑的数据库 还有个简单的办法,你可以在VS的服务器资源管理器里新建一个数据库连接

沧浪区15750193347: C#链接SQL2005
兴信启脾: SqlConnection Conn = new SqlConnection(Str); comStr = "SQL语句" SqlCommand Comm = new SqlCommand(comStr, Conn); Conn.Open(); Comm.ExecuteNonQuery(); Conn.Close(); string Str = "data source=(local);database=数据库名;user id=sa;password=sa";

沧浪区15750193347: 如何把C#2005连接SQL2005的语句改成C#2005连接SQL2000的语句?
兴信启脾: 其实代码是一样的,但是不同机子或者同一个机子的服务器的名称可能不一样,要改的话只需要改conn = new SqlConnection("server=(local)\\sqlexpress;integrated security=true;database=studentDB");中的server=(local)\\sqlexpress;=号后面的就是服务器的名称,如果电脑上面只安装了一个SQL的话,就可以直接改成server=.;代表本机服务器

沧浪区15750193347: C# 窗体连接sql2005 数据库,验证用户名和密码是否正确 用什么控件 和代码段 -
兴信启脾: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Lin...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网