在.NET Winform应用程序中进行数据查询,通常有以下几种方法:
using System;
using System.Data;
using System.Data.SqlClient;
namespace WinformDataQuery
{
public partial class Form1 : Form
{
private string connectionString = "your_connection_string";
public Form1()
{
InitializeComponent();
}
private void btnQuery_Click(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "SELECT * FROM your_table";
using (SqlCommand command = new SqlCommand(query, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// 读取数据并显示在控件中,例如:
// txtId.Text = reader["Id"].ToString();
// txtName.Text = reader["Name"].ToString();
}
}
}
}
}
}
}
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using YourNamespace.Models; // 替换为你的实体模型命名空间
using YourNamespace.DbContext; // 替换为你的DbContext命名空间
namespace WinformDataQuery
{
public partial class Form1 : Form
{
private YourDbContext dbContext;
public Form1()
{
InitializeComponent();
dbContext = new YourDbContext();
}
private async void btnQuery_Click(object sender, EventArgs e)
{
var results = await dbContext.YourTable
.ToListAsync(); // 替换为你的表名
// 处理查询结果,例如:
foreach (var item in results)
{
// 显示数据在控件中,例如:
// txtId.Text = item.Id.ToString();
// txtName.Text = item.Name.ToString();
}
}
}
}
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Dapper;
namespace WinformDataQuery
{
public partial class Form1 : Form
{
private string connectionString = "your_connection_string";
public Form1()
{
InitializeComponent();
}
private async void btnQuery_Click(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "SELECT * FROM your_table";
StringBuilder sqlBuilder = new StringBuilder(query);
var results = await connection.QueryAsync<YourEntity>(sqlBuilder.ToString()); // 替换为你的实体类名
// 处理查询结果,例如:
foreach (var item in results)
{
// 显示数据在控件中,例如:
// txtId.Text = item.Id.ToString();
// txtName.Text = item.Name.ToString();
}
}
}
}
}
这些方法都可以用于在.NET Winform应用程序中进行数据查询。你可以根据自己的需求和喜好选择合适的方法。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。