• 1、winform数据库调用(基本方法)


    //向数据库中新增数据 
        private
    void button1_Click_1(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=作业;user=sa;pwd=123"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "insert into student values(@sno,@sname,@ssex,@sbirthday,@class)"; cmd.Parameters.Clear(); cmd.Parameters.Add("@sno",textbox1.Text.Trim()); cmd.Parameters.Add("@sname", textbox2.Text.Trim()); if (rbtnan.Checked) { cmd.Parameters.Add("@ssex", ""); } else { cmd.Parameters.Add("@ssex", ""); } cmd.Parameters.Add("@sbirthday", dateTimePicker1.Value); cmd.Parameters.Add("@class", comboBox1.Text.Trim()); conn.Open(); int count = cmd.ExecuteNonQuery(); if (count > 0) { MessageBox.Show("执行成功,受影响行数为:" + count.ToString()); } else { MessageBox.Show("执行失败!"); } conn.Close(); }
    //修改数据库中的数据
         private
    void button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=作业;user=sa;pwd=123"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "update student set sname=@sname,ssex=@ssex,sbirthday=@sbirthday,class=@class where sno=@sno"; cmd.Parameters.Clear(); cmd.Parameters.Add("@sno", textbox1.Text.Trim()); cmd.Parameters.Add("@sname", textbox2.Text.Trim()); if (rbtnan.Checked) { cmd.Parameters.Add("@ssex", ""); } else { cmd.Parameters.Add("@ssex", ""); } cmd.Parameters.Add("@sbirthday", dateTimePicker1.Value); cmd.Parameters.Add("@class", comboBox1.Text.Trim()); conn.Open(); int count = cmd.ExecuteNonQuery(); if (count > 0) { MessageBox.Show("执行成功,受影响行数为:" + count.ToString()); } else { MessageBox.Show("执行失败!"); } conn.Close(); }
    //删除数据库中的数据
         private
    void button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=作业;user=sa;pwd=123"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "Delete from student where sno=@sno"; cmd.Parameters.Clear(); cmd.Parameters.Add("@sno", textbox1.Text.Trim()); conn.Open(); int count = cmd.ExecuteNonQuery(); if (count > 0) { MessageBox.Show("执行成功,受影响行数为:" + count.ToString()); } else { MessageBox.Show("执行失败!"); } conn.Close(); }
    //查询数据库中的数据
         private
    void button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=作业;user=sa;pwd=123"); SqlCommand cmd = conn.CreateCommand(); DataTable dt = new DataTable(); cmd = conn.CreateCommand(); cmd.CommandText = "select * from student"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); for (int i = 0; i < dr.FieldCount; i++) { dt.Columns.Add(dr.GetName(i)); } while (dr.Read()) { DataRow row = dt.NewRow(); row["sno"] = dr["sno"]; row["sname"] = dr["sname"]; row["ssex"] = dr["ssex"]; row["sbirthday"] = dr["sbirthday"]; dt.Rows.Add(row); } conn.Close(); }
  • 相关阅读:
    ReactiveCocoa 谈谈RACMulticastConnection
    ReactiveCocoa 谈谈concat
    Swift 一些环境配置
    hpple 简单使用
    Swift 学习手记1,pod 的 类库使用
    [转]SQL语句:Group By总结
    Jquery VailDate初探
    C#RSA加密解密详解
    电子印章制作管理系统 -升级版本
    tensorflow 实现的第一个目标检测模型
  • 原文地址:https://www.cnblogs.com/XMH1217423419/p/4298605.html
Copyright © 2020-2023  润新知