c#接上:连接查询数据库获取当天的良品条码记录
时间: 2023-10-07 20:13:22 浏览: 50
假设你使用的是 SQL Server 数据库,以下是一个示例代码:
```csharp
using System;
using System.Data.SqlClient;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
// 连接数据库
using (SqlConnection connection = new SqlConnection("Data Source=服务器名或IP地址;Initial Catalog=数据库名;Integrated Security=True"))
{
// 打开连接
connection.Open();
// 查询当天的良品条码记录
string sql = "SELECT Barcode FROM Production WHERE Convert(date, ProductionDate) = Convert(date, GetDate()) AND Quality = 'OK'";
// 执行查询
using (SqlCommand command = new SqlCommand(sql, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
// 读取结果
while (reader.Read())
{
string barcode = reader.GetString(0);
Console.WriteLine(barcode);
}
}
}
}
Console.ReadLine();
}
}
}
```
请注意替换上述代码中的服务器名、数据库名以及查询语句以符合你的实际需求。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![dll](https://img-home.csdnimg.cn/images/20210720083646.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)