c# sql server 与dataGridView 绑定数据

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;


namespace 数据统计项目
{
    public partial class Form1 : Form
    {
        string serverip;//数库存的IP
        string uid;//数据库的用户名
        string upass;//数据库的密码
        string dbname;//数据库名称
        string sql;//自定义SQL
        string txt;//提示信息
        //SqlConnection Conn;//数据库连接对象
        //SqlCommand cmd;
        //DataSet ds;
        //SqlDataAdapter sda;
        string sqlstring;


        public Form1()
        {          
            InitializeComponent();
            serverip= textBox1.Text;
            dbname = textBox2.Text;
            uid = textBox3.Text;
            upass = textBox4.Text;
            button1.Enabled = true;
            comboBox1.Enabled = false;
            sqlstring = "Server=" + textBox1.Text + ";DataBase=" + textBox2.Text + ";uid=" + textBox3.Text + ";pwd=" + textBox4.Text;
            // MessageBox.Show(serverip + ":" + dbname + ":" + uid + ":" + upass, "提示");//连接字符串


        }


        private void label1_Click(object sender, EventArgs e)
        {


        }


        private void Form1_Load(object sender, EventArgs e)
        {


        }


        private void label2_Click(object sender, EventArgs e)
        {


        }


        private void label4_Click(object sender, EventArgs e)
        {


        }


        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {


        }


        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = true;
            comboBox1.Enabled = false;
            SqlConnection Conn = new SqlConnection(sqlstring);//新建连接对象


            Conn.Open();//打开连接
            if (Conn.State == ConnectionState.Open)
            {
                textBox5.Text = "数据库连接成功!";
                button1.Enabled = false;
                textBox6.Enabled = true;
                button2.Enabled = true;
                comboBox1.Enabled = true;
                textBox1.Enabled = false;
                textBox2.Enabled = false;
                textBox3.Enabled = false;
                textBox4.Enabled = false;
            }
            else
            {
                textBox5.Text = "数据库连接失败!";
                button1.Enabled = true;
                textBox6.Enabled = false;
                button2.Enabled = false;
                comboBox1.Enabled = false;
                textBox1.Enabled = true;
                textBox2.Enabled = true;
                textBox3.Enabled = true;
                textBox4.Enabled = true;
                return ;
            }
           
        }


        private void textBox5_TextChanged(object sender, EventArgs e)
        {


        }


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


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


        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection Conn = new SqlConnection(sqlstring);//新建连接对象


            Conn.Open();//打开连接
            //MessageBox.Show(comboBox1.SelectedIndex.ToString());
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(sql, Conn);//新建查询
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            int count =sda.Fill(ds, comboBox1.SelectedIndex.ToString());
            if (count == 0)
            {
                return;
            }
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = comboBox1.SelectedIndex.ToString();
            /* for (int i = 0; i < sqlrow - 1; i++)
             {
                 dataGridView1.Rows[i].Cells[0].Value = i ;  //id列显示成序列号,从1开始
             }*/
            Conn.Close();
            Conn.Dispose();
        }


        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {


        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                sql = textBox6.Text;
            }
            else if (comboBox1.SelectedIndex == 1)
            {
                sql = "select * from d_hr_源物料";
            }
            else if (comboBox1.SelectedIndex == 2)
            {
                sql = "select * from d_hr_源供应商头";
            }
            else if (comboBox1.SelectedIndex == 3)
            {
                sql = "select * from d_hr_源供应商地点";
            }
            else if (comboBox1.SelectedIndex == 4)
            {
                sql = "select * from d_hr_源客户头";
            }
            else if (comboBox1.SelectedIndex == 5)
            {
                sql = "select * from d_hr_源客户地点";
            }
            else if (comboBox1.SelectedIndex == 6)
            {
                sql = "exec  p_hr_商品校验 ";
            }
        }
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值