数据库修改
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ConnSqlForm
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private string _Xuehao;
private string _Name;
private string _Sex;
private string _Birthday;
public Form2(string xuehao,string name,string sex,string birthday)
{
InitializeComponent();
_Xuehao = xuehao;
_Name = name;
_Sex = sex;
_Birthday = birthday;
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=snewdata;user=sa;pwd=");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "update student set name=@name,sex=@sex,birthday=@birthday where xuehao=@xuehao";
cmd.Parameters.Clear();
cmd.Parameters.Add("@xuehao", txtxuehao.Text.Trim());
cmd.Parameters.Add("@name", txtname.Text.Trim());
if (rbtnan.Checked)
{
cmd.Parameters.Add("@sex", "男");
}
else
{
cmd.Parameters.Add("@sex", "女");
}
cmd.Parameters.Add("@birthday", dateTimePicker1.Value);
conn.Open();
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("修改成功,受影响行数为:" + count.ToString());
}
else
{
MessageBox.Show("修改失败!");
}
conn.Close();
}
private void Form2_Load(object sender, EventArgs e)
{
txtxuehao.Text = _Xuehao;
txtname.Text = _Name;
if (_Sex == "男")
{
rbtnan.Checked = true;
}
else
{
rbtnv.Checked = true;
}
dateTimePicker1.Value = DateTime.Parse(_Birthday);
}
}
}