C#一个文本框输入查询后,多个文本框显示多条来自数据库中的相应内容

C#一个文本框输入查询后,多个文本框显示多条来自数据库中的相应内容,第1张

 private void button1_Click(object sender, EventArgs e)

        {

            string conn = "Data Source=.\\SQLEXPRESSInitial Catalog=E_ReadingroomIntegrated Security=SSPI"//连接数据库信息

            SqlConnection connection = new SqlConnection(conn)                     //创建连接

            connection.Open()                                                      //打开连接

            string sql = string.Format("select name,dept from SysA where ID='{0}'",textBox1.Text )

            SqlCommand comm = new SqlCommand(sql, connection)                     //command对象          

            SqlDataReader dr = comm.ExecuteReader()                               //定义数据读取对象

            if (dr.Read())

            {

                this.textBox2.Text = (string)dr.GetValue(0)

                this.textBox3.Text = (string)dr.GetValue(1)

            }

            connection.Close()

        }

PS:没测试过,SQL语句改了 下,textBox.Text赋值改了下。测下看看是不是你要的效果

第一步,先建立与数据库的连接

第二步,在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=SWALLUser ID=SAPassword=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项目名

private 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

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6714397.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存