想问下我用的是windowsform做的一个学生信息系统管理界面其中有添加查询和删除更新几个按钮..想求教这几个按钮的代码该如何写..刚学与数据库交互使用还不是太熟练...

解决方案 »

  1.   

    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 WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                string txt1 = this.textBox1.Text ;
                string txt2 = this.textBox2.Text;
                string txt3 = this.textBox3.Text;
                SqlConnection con = new SqlConnection("Data Source=FZ-53\\SQL2005;Initial Catalog=ACCP;Integrated Security=True");
                con.Open();
                MessageBox.Show("数据库连接成功!");
                SqlCommand com = new SqlCommand("insert into students(SAddress,SName,SGrade) values('" + txt1 + "','" + txt2 + "','" + txt3 + "') ", con);
                com.ExecuteNonQuery();
                MessageBox.Show("数据库修改成功!");
                con.Close();
                MessageBox.Show("数据库关闭成功!");
            }
        }
    }
      

  2.   

    string strConn = "Server = servername;Database = DBName; UID = username;PWD = password;";
    SqlConnection conn = new SqlConnection(strConn);
    string strCmd = "delete from tablename";
    conn.Open();
    SqlCommand cmd = new SqlCommand(strCmd,conn);
    cmd.ExcuteNoneQuery();
    conn.Close();
    cmd.Dispose();
    随后敲的,有些地方可能没区分大小写
      

  3.   

    顺便问下当我在添加好相应的数据后想点击保存按钮后这个添加窗口消失并且把刚才添加的数据显示在相应的信息界面中的dataGridView中这段代码该如何写呢..求好人赐教...
      

  4.   

    最好把数据库添加删除这些命令写到数据库中,弄成存储过程,然后command调用这些存储过程 这样速度快