C#连接数据库,更新,查找

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;



namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        /*
          SqlConnection conn;
          string constr = "server=ACER-PC\\LI;database=db_test;uid=sa;pwd=123";
          conn = new SqlConnection(constr);  //数据库连接  (后两句放到函数中)
        */

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        
        private void button3_Click(object sender, EventArgs e)
        {
            string connString = @"Data Source=.\sqlexpress; Initial Catalog=hongyuan;Integrated Security=true";
            SqlConnection conn = new SqlConnection(connString);
            string sql = "select * from admin";
            //string sql = String.Format("Select count(*) from [admin] where username='{0}'and password='{1}'", userName, passWord);
 
            try
            {
                //string[] mess = new string[]{};
                string mess ="mess:";
                conn.Open();                
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlDataReader reader = comm.ExecuteReader();

                
                    while(reader.Read())
                    {
                       
                            int i = 0;
                            mess = mess + reader.GetString(i);
                            //Console.WriteLine(reader.GetString(i));
                           // i++;
                        
                        
                    }

                    textBox2.Text = mess;
                conn.Close();
                
                
                
                /*int n = (int)comm.ExecuteScalar();
                if (n == 1)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Tag = true;
                }
                else
                {
                    MessageBox.Show("您输入的用户名或密码错误!请好好想想", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.Tag = false;
                }*/

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "操作数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.Tag = false;
            }
            finally
            {
                conn.Close();
            }
             

        }

        private void OutPut_Click(object sender, EventArgs e)
        {
          
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string aaa = textBox3.Text.ToString();
            string connString = @"Data Source=.\sqlexpress; Initial Catalog=hongyuan;Integrated Security=true";
            string sql = "insert into admin(aaa) values('" + aaa + "')";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);
                int n = comm.ExecuteNonQuery();
                if (n > 0)
                {
                    string mess1 = "添加成功";
                    label2.Text = mess1;
                }
                else
                {
                    string mess2 = "添加失败";
                    label2.Text = mess2;
                }
                conn.Close();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

猜你喜欢

转载自blog.csdn.net/da___vinci/article/details/79720173