winform查询数据库

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

namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string str = comboBox1.SelectedItem.ToString();
if (str == "TEST_D")
{

//这里用的是本地数据库

 

string ConnectString = "server =Q1812LN0002\\SQLEXPRESS;database =TEST_D; Trusted_Connection=true";
string sql = "SELECT * FROM CYCLER_ERROR_E_DATE_ID WITH(NOLOCK)";

SqlConnection conn = new SqlConnection();

conn.ConnectionString = ConnectString;

conn.Open();

SqlDataAdapter data = new SqlDataAdapter(sql, conn);

DataSet dt = new DataSet();

data.Fill(dt, "table1");

DataTable datable = dt.Tables["table1"];

dataGridView1.DataSource = datable;

conn.Close();
}
else if (str == "TEST_C")
{
string ConnectString = "server =Q1812LN0002\\SQLEXPRESS;database =TEST_C; Trusted_Connection=true";
string sql = "SELECT * FROM DATE_ID WITH(NOLOCK)";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConnectString;
conn.Open();

SqlDataAdapter data = new SqlDataAdapter(sql, conn);
DataSet dt = new DataSet();
data.Fill(dt, "table1");
DataTable datable = dt.Tables["table1"];
dataGridView1.DataSource = datable;
conn.Close();
}
else if (str == "TEST_B")
{
string ConnectString = "server =Q1812LN0002\\SQLEXPRESS;database =TEST_B; Trusted_Connection=true";
string sql = "SELECT * FROM CYCLER_ERROR_E WITH(NOLOCK)";
SqlConnection conn = new SqlConnection(ConnectString);
conn.Open();

SqlDataAdapter data = new SqlDataAdapter(sql, conn);
DataSet dt = new DataSet();
data.Fill(dt, "table1");
DataTable datable = dt.Tables["table1"];
dataGridView1.DataSource = datable;
conn.Close();
}
else
{
string ConnectString = "server =Q1812LN0002\\SQLEXPRESS;database =TEST_A; Trusted_Connection=true";
string sql = "SELECT * FROM CYCLER_FLAT_ERROR WITH(NOLOCK)";

SqlConnection conn = new SqlConnection();
//实例化连接对象
//也可以写成SqlConnection conn = new SqlConnection(ConnString);

conn.ConnectionString = ConnectString;
//设置连接字符串

conn.Open();
//连接数据库

SqlDataAdapter data = new SqlDataAdapter(sql, conn);
//实例化adapter的对象

DataSet dt = new DataSet();
//创建dataSet

data.Fill(dt, "table1");
// 将返回的数据集作为“表”填入DataSet中

DataTable datable = dt.Tables["table1"];
//将本地的table1这个表传给datable, DataSet是一个“表”的集合,而DataTable只是一张表。

dataGridView1.DataSource = datable;
//显示数据表,注意DataSet是不能用于dataGridView控件显示

conn.Close();
}

 

 

 

 

// static void Main(string[] args)
// {
// //新建一个数据库连接
// using (SqlConnection conn = new SqlConnection(GetConnectString()))
// {
// conn.Open();//打开数据库
// //Console.WriteLine("数据库打开成功!");
// //创建数据库命令
// SqlCommand cmd = conn.CreateCommand();
// //创建查询语句
// cmd.CommandText = "SELECT * FROM userinfo";
// //从数据库中读取数据流存入reader中
// SqlDataReader reader = cmd.ExecuteReader();

// //从reader中读取下一行数据,如果没有数据,reader.Read()返回flase
// while (reader.Read())
// {
// //reader.GetOrdinal("id")是得到ID所在列的index,
// //reader.GetInt32(int n)这是将第n列的数据以Int32的格式返回
// //reader.GetString(int n)这是将第n列的数据以string 格式返回
// int id = reader.GetInt32(reader.GetOrdinal("id"));
// string name = reader.GetString(reader.GetOrdinal("name"));
// string pwd = reader.GetString(reader.GetOrdinal("password"));
// int age = reader.GetInt32(reader.GetOrdinal("age"));
// string sex = reader.GetString(reader.GetOrdinal("sex"));
// string phone = reader.GetString(reader.GetOrdinal("phone"));
// string address = reader.GetString(reader.GetOrdinal("Address"));

// //格式输出数据
// Console.Write("ID:{0},Name:{1},PWD:{2},Age:{3},Sex:{4},Phone{5},Address:{6}\n", id, name, pwd, age, sex, phone, address);
// }
// }
// Console.ReadKey();
// }
// //得到一个数据库连接字符串
// static string GetConnectString()
// {
// return "Data Source=(local);Initial Catalog=db1;Integrated Security=SSPI;";
// }
//}

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}
}
}

posted @ 2021-07-02 11:32  Lee597  阅读(570)  评论(1编辑  收藏  举报