C#进行Sqlserver数据库增删查改操作

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C#连接SQL Server数据库并进展增删查改操作详细代码在下边,直接可以使用
设计好的C#界面:
共3个窗体。

数据库:
运行后的界面:
代码:
〔Form1〕
publicpartialclass Form1 : Form
{
public Form1()
{
Initializeponent();
}
privatevoid button1_Click(object sender, EventArgs e) {
Form1_Load(sender, e);
}
privatevoid Form1_Load(object sender, EventArgs e) {
try
{
SqlConnection conn = new SqlConnection();
string str = "Data Source=ZWEIQUAN-PC;Initial Catalog=Student;Integrated Security=True";
conn.ConnectionString = str;
conn.Open();
Sqlmand cmd = new Sqlmand();
cmd.mandText = "select * from 根本信息表";
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, "根本信息表");
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = "根本信息表";
this.dataGridView1.AutoGenerateColumns = true;
for (int i = 1; i < this.dataGridView1.ColumnCount; i++)
{
this.dataGridView1.Columns[i].DefaultCellStyle.SelectionBackColor = Color.White;
this.dataGridView1.Columns[i].DefaultCellStyle.SelectionForeColor = Color.Black;
this.dataGridView1.Columns[i].ReadOnly = true;
}
conn.Close();
}
catch(Exception ee)
{
MessageBox.Show("错误:"+ee.Message
,"错误");
}
}
privatevoid button2_Click(object sender, EventArgs e)
{
if (this.dataGridView1.SelectedCells==null)
{
MessageBox.Show("请选择要删¦除的项!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if (this.dataGridView1.CurrentCell.ColumnIndex == 0)
{
string st = this.dataGridView1[1, this.dataGridView1.CurrentCell.RowIndex].Value.ToString(); SqlConnection conn = new SqlConnection();
string str = "Data Source=ZWEIQUAN-PC;Initial Catalog=Student;Integrated Security=True";
conn.ConnectionString = str;
conn.Open();
Sqlmand cmd = new Sqlmand();
cmd.mandText = "delete from 根本信息表where ='" + st + "'";
cmd.Connection = conn;
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
privatevoid button3_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Trim() == "")
{
MessageBox.Show("请输入要查询的!", "提示");
}
else
{
SqlConnection conn = new SqlConnection();
string str = "Data Source=ZWEIQUAN-PC;Initial Catalog=Student;Integrated Security=True";
conn.ConnectionString = str;
conn.Open();
Sqlmand cmd = new Sqlmand();
cmd.mandText = "select * from 根本信息表where ='" +
this.textBox1.Text.Trim() + "'";
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = ds.Tables[0].ToString();
this.dataGridView1.AutoGenerateColumns = true;
conn.Close();
}
}
privatevoid button4_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
privatevoid button5_Click(object sender, EventArgs e)
{
Form3 f = new Form3();
f.id = Convert.ToInt32(this.dataGridView1[0,
this.dataGridView1.CurrentCell.RowIndex].Value.ToString());
f.Show();
}
}
(Form2)
publicpartialclass Form2 : Form
{
public Form2()
{
Initializeponent();
}
privatevoid Form2_Load(object sender, EventArgs e)
{
boBox1.Items.Add("男");
boBox1.Items.Add("女");
boBox2.Items.Add("计算机11-1班");
boBox2.Items.Add("计算机11-2班");
boBox2.Items.Add("计算机11-3班");
boBox1.DropDownStyle = boBoxStyle.DropDownList;
boBox2.DropDownStyle = boBoxStyle.DropDownList;
get_bh();
}
privatevoid get_bh()
{
SqlConnection conn = new SqlConnection();
string str = "Data Source=ZWEIQUAN-PC;Initial Catalog=Student;Integrated Security=True";
conn.ConnectionString = str;
conn.Open();
Sqlmand cmd = new Sqlmand();
cmd.mandText = "select * from 根本信息表";
//cmd.mandText = "select isnull(max(编号),0)+1 from 根本信息表";
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
int t = ds.Tables[0].Rows.Count;
int bh = t - 1;
this.textBox1.Text = (Int32.Parse(ds.Tables[0].Rows[bh]["编号"].ToString()) + 1).ToString(); //this.textBox1.Text = cmd.ExecuteScalar().ToString();
conn.Close();
}
privatevoid button1_Click(object sender, EventArgs e)
{
if (textBox2.Text.Trim() == "" || textBox3.Text.Trim() == "" || boBox1.Text.Trim() == "" || boBox2.Text.Trim() == "")
{
MessageBox.Show("请输入完整信息!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
string str1 = textBox3.Text.Trim();
int l = str1.Length;
for (int i = 0; i < l; i++)
{
if (!char.IsNumber(str1[i]))
{
MessageBox.Show("年龄输入有误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox3.SelectAll();
textBox3.Focus();
return;
}
}
SqlConnection conn = new SqlConnection();
string str = "Data Source=ZWEIQUAN-PC;Initial Catalog=Student;Integrated Security=True";
conn.ConnectionString = str;
conn.Open();
Sqlmand cmd = new Sqlmand();
cmd.mandText = "insert into 根本信息表(编号,,性别,年龄,班级)values(编号,,性别,年龄,班级)";
cmd.Connection = conn;
cmd.Parameters.Add("编号", SqlDbType.Int);
cmd.Parameters.Add("", SqlDbType.NVarChar, 50);
cmd.Parameters.Add("性别", SqlDbType.NVarChar, 50);
cmd.Parameters.Add("年龄", SqlDbType.Int);
cmd.Parameters.Add("班级", SqlDbType.NVarChar, 50);
cmd.Parameters["编号"].Value = textBox1.Text.Trim();
cmd.Parameters[""].Value = textBox2.Text.Trim();
cmd.Parameters["性别"].Value = boBox1.Text.Trim();
cmd.Parameters["年龄"].Value = textBox3.Text.Trim();
cmd.Parameters["班级"].Value = boBox2.Text.Trim();
cmd.ExecuteNonQuery();
conn.Close();
this.Close();
}
}
privatevoid button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
(Form3)
publicpartialclass Form3 : Form
{
public Form3()
{
Initializeponent();
}
publicint id;
privatevoid Form3_Load(object sender, EventArgs e)
{
textBox1.Text = id.ToString();
SqlConnection conn = new SqlConnection("Data Source=ZWEIQUAN-PC;Initial Catalog=Student;Integrated Security=True");
Sqlmand cmd = new Sqlmand();
cmd.Connection = conn;
conn.Open();
cmd.mandText = "select * from 根本信息表where 编号=" + id.ToString(); SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds.Tables[0].Rows.Count == 1)
{
this.textBox2.Text = (ds.Tables[0].Rows[0][""].ToString());
this.textBox3.Text = (ds.Tables[0].Rows[0]["性别"].ToString());
this.textBox4.Text = (ds.Tables[0].Rows[0]["年龄"].ToString());
this.textBox5.Text = (ds.Tables[0].Rows[0]["班级"].ToString());
conn.Close();
}
}
privatevoid button1_Click(object sender, EventArgs e)
{
if (textBox2.Text.Trim() == "" || textBox3.Text.Trim() == "" || textBox4.Text.Trim() == "" || textBox5.Text.Trim() == "")
{
MessageBox.Show("请输入完整信息!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
string str1 = textBox4.Text.Trim();
int l = str1.Length;
for (int i = 0; i < l; i++)
{
if (!char.IsNumber(str1[i]))
{
MessageBox.Show("年龄输入有误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox4.SelectAll();
textBox4.Focus();
return;
}
}
SqlConnection conn = new SqlConnection();
string str = "Data Source=ZWEIQUAN-PC;Initial Catalog=Student;Integrated Security=True";
conn.ConnectionString = str;
conn.Open();
Sqlmand cmd = new Sqlmand();
cmd.mandText = "update 根本信息表set 编号=编号,=,性别=性别,年龄=年龄,班级=班级where 编号=编号";
cmd.Connection = conn;
cmd.Parameters.Add("编号", SqlDbType.Int);
cmd.Parameters.Add("", SqlDbType.NVarChar, 50);
cmd.Parameters.Add("性别", SqlDbType.NVarChar, 50);
cmd.Parameters.Add("年龄", SqlDbType.Int);
cmd.Parameters.Add("班级", SqlDbType.NVarChar, 50);
cmd.Parameters["编号"].Value = textBox1.Text.Trim();
cmd.Parameters[""].Value = textBox2.Text.Trim();
cmd.Parameters["性别"].Value = textBox3.Text.Trim();
cmd.Parameters["年龄"].Value = textBox4.Text.Trim();
cmd.Parameters["班级"].Value = textBox5.Text.Trim();
cmd.ExecuteNonQuery();
conn.Close();
this.Close();
}
}
privatevoid button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
注:"Data Source=ZWEIQUAN-PC;Initial Catalog=Student;Integrated Security=True" ZWEIQUAN-PC:计算机名
Student:对应的数据库名称。

相关文档
最新文档