基本信息
源码名称:C#通过视图操作管理数据库实例,mssql数据库
源码大小:0.46M
文件格式:.zip
开发语言:C#
更新时间:2013-01-26
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
视图操作管理数据库
public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region 定义公共的类对象及变量 SqlConnection sqlcon; //声明数据库连接对象 SqlDataAdapter sqlda; //声明数据桥接器对象 DataSet myds; //声明数据集对象 //定义数据库连接字符串 string strCon = "Data Source=(local);Database=db_09;uid=sa;pwd=;"; #endregion private void Form1_Load(object sender, EventArgs e) { cboxSex.SelectedIndex = 0; ShowInfo(); } private void dgvInfo_CellClick(object sender, DataGridViewCellEventArgs e) { //记录选择的职工编号 string strID = dgvInfo.Rows[e.RowIndex].Cells[0].Value.ToString(); if (strID != "") { sqlcon = new SqlConnection(strCon); //实例化数据库连接对象 //实例化数据桥接器对象 sqlda = new SqlDataAdapter("select * from View_Info where ID='" strID "'", sqlcon); myds = new DataSet(); //实例化数据集对象 sqlda.Fill(myds); //填充数据集 //显示职工编号 txtID.Text = myds.Tables[0].Rows[0][0].ToString(); //显示职工姓名 txtName.Text = myds.Tables[0].Rows[0][1].ToString(); //显示职工性别 cboxSex.Text = myds.Tables[0].Rows[0][2].ToString(); //显示职工年龄 txtAge.Text = myds.Tables[0].Rows[0][3].ToString(); //显示职工电话 txtTel.Text = myds.Tables[0].Rows[0][4].ToString(); //显示职工家庭地址 txtAddress.Text = myds.Tables[0].Rows[0][5].ToString(); //显示职工QQ txtQQ.Text = myds.Tables[0].Rows[0][6].ToString(); //显示职工Email txtEmail.Text = myds.Tables[0].Rows[0][7].ToString(); //使用数据库中存储的二进制头像实例化内存数据流 MemoryStream MStream = new MemoryStream((byte[])myds.Tables[0].Rows[0][8]); pictureBox1.Image = Image.FromStream(MStream); //显示职工头像 } } private void btnEdit_Click(object sender, EventArgs e) { if (txtID.Text != "") { //修改职工信息 if (UpdateInfo(txtID.Text, txtName.Text, cboxSex.Text, Convert.ToInt32(txtAge.Text), txtTel.Text, txtAddress.Text, Convert.ToInt32(txtQQ.Text), txtEmail.Text)) { MessageBox.Show("通过视图修改职工信息成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } ShowInfo(); } private void btnCancel_Click(object sender, EventArgs e) { foreach (Control ctl in groupBox1.Controls) { if (ctl.GetType().Name == "TextBox") ctl.Text = ""; } txtID.Focus(); } #region 修改职工信息 /// <summary> /// 修改职工信息 /// </summary> /// <param name="strID">职工编号</param> /// <param name="strName">职工姓名</param> /// <param name="strSex">性别</param> /// <param name="intAge">年龄</param> /// <param name="strTel">电话</param> /// <param name="strAddress">家庭地址</param> /// <param name="intqq">QQ</param> /// <param name="strEmail">Email</param> /// <returns>执行成功,返回true</returns> private bool UpdateInfo(string strID,string strName,string strSex,int intAge,string strTel,string strAddress,int intqq,string strEmail) { sqlcon = new SqlConnection(strCon); SqlCommand sqlcmd = new SqlCommand("update View_Info set Name=@name,Sex=@sex,Age=@age,Tel=@tel,Address=@address,QQ=@qq,Email=@email where ID=@id", sqlcon); sqlcmd.Parameters.Add("@id", SqlDbType.VarChar, 20).Value = strID; sqlcmd.Parameters.Add("@name", SqlDbType.VarChar, 30).Value = strName; sqlcmd.Parameters.Add("@sex", SqlDbType.Char, 4).Value = strSex; sqlcmd.Parameters.Add("@age", SqlDbType.Int, 4).Value = intAge; sqlcmd.Parameters.Add("@tel", SqlDbType.VarChar, 20).Value = strTel; sqlcmd.Parameters.Add("@address", SqlDbType.VarChar, 100).Value = strAddress; sqlcmd.Parameters.Add("@qq", SqlDbType.BigInt, 8).Value = intqq; sqlcmd.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = strEmail; sqlcon.Open(); sqlcmd.ExecuteNonQuery(); sqlcon.Close(); return true; } #endregion #region 在DataGridView中显示职工基本信息 /// <summary> /// 在DataGridView中显示职工基本信息 /// </summary> private void ShowInfo() { sqlcon = new SqlConnection(strCon); sqlda = new SqlDataAdapter("select ID as 职工编号,Name as 职工姓名,Sex as 性别,Age as 年龄,Tel as 联系电话,Address as 家庭地址,QQ as QQ号码,Email as Email地址 from View_Info", sqlcon); myds = new DataSet(); sqlda.Fill(myds); dgvInfo.DataSource = myds.Tables[0]; } #endregion }