c# sql server 與dataGridView 繫結資料
阿新 • • 發佈:2019-02-12
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_商品校驗 ";
}
}
}
}
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_商品校驗 ";
}
}
}
}