C#里怎么把SQL里的数据显示在文本框里?
第一步,先建立与数据库的连接
第二步,在program.cs里面写程序
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CaterDal
{
public static class SqliteHelper
{
//从配置文本中读取连接字符串
private static string connStr = "Data Source=数据库的名称;Initial Catalog=SWALL;User ID=SA;Password=666";//是这个数据库没错吧?
public static DataTable GetDataTable(string sql,params SqlParameter[] ps)
{
using (SqlConnection conn=new SqlConnection(connStr))
{
//构造适配器对象
SqlDataAdapter pter=new SqlDataAdapter(sql,conn);
//构造数据表,用于接收查询结果
DataTable dt=new DataTable();
//添加参数
pter.SelectCommand.Parameters.AddRange(ps);
//执行结果
pter.Fill(dt);
//返回结果集
return dt;
}
}
}
}
第三步,在项目里面写程序
private void button4_Click(object sender, EventArgs e)
{
DataTable Dt = SqliteHelper.GetDataTable("select * from jjjj");
dataGridView1.DataSource = Dt;
}
}
}
private void button4_Click(object sender, EventArgs e)
{
DataTable Dt = SqliteHelper.GetDataTable("select * from jjjj");
dataGridView1.DataSource = Dt;
}
}
}
private void button4_Click(object sender, EventArgs e)
{
DataTable Dt = SqliteHelper.GetDataTable("select * from lishiji");
dataGridView1.DataSource = Dt;
}
}
}
Form1.cs项目名
1private void button4_Click(object sender, EventArgs e)
{
DataTable Dt = SqliteHelper.GetDataTable("select * from jjjj");
dataGridView1.DataSource = Dt;
}
}
}
FROM2.CS项目名
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using CaterDal;
namespace sqlssss
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataTable Dt = new DataTable();
Dt = SqliteHelper.GetDataTable("select * from lishiji");
DgvQueue.DataSource = Dt;
}
}
}