需要导入小盒码的表格模板后,从数据库里面根据小盒码表格内的数据,查询出其他关联信息,以下是我的查询代码,模板的截图如下,现在导入后,查询没到内容,若单个将小盒码直接写在代码中,是可以查询出来的,请看看有什么问题吗,谢谢;
private void btncheck_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog
{
Title = "选择Excel文件",
Filter = "Excel文件|*.xls;*.xlsx;*.xlsm",
Multiselect = false
};
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// 获取用户选择的文件路径
string filePath = openFileDialog1.FileName;
try
{
using (var package = new ExcelPackage(new FileInfo(filePath)))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
// 获取工作表的行数和列数
int rowCount = worksheet.Dimension.Rows;
int colCount = worksheet.Dimension.Columns;
// 创建 DataTable 列
for (int col = 1; col <= colCount; col++)
{
dt.Columns.Add(worksheet.Cells[1, col].Text); // 使用第一行作为列名
}
// 创建 DataTable 行
for (int row = 2; row <= rowCount; row++)
{
var newRow = dt.NewRow();
for (int col = 1; col <= colCount; col++)
{
newRow[col - 1] = worksheet.Cells[row, col].Text; // 获取单元格的文本值
}
dt.Rows.Add(newRow);
}
}
// 遍历每个“小盒码”并查询数据库
foreach (DataRow row in dt.Rows)
{
string smallBoxCode = row["小盒码"].ToString();
// 执行数据库查询操作
string connectionString = "server=192.168.4.200;database=BCM30_DrugAdmin;user=sa;password=sskyadmin1!";
string query = @"SELECT dr.batchNo, dr.parentCode2, wo.orderCode,wd.customerName,wd.orderType,wo.createTime,wo.updateTime FROM dbo.da_codeRelation_193815500501 dr
JOIN dbo.da_warehouseOutCode wo ON dr.parentCode2 = wo.code20
JOIN dbo.bcm_warehouseOrder wd on wo.orderCode = wd.orderCode
WHERE dr.code20 = @SmallBoxCode";
using (SqlConnection Conn = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(query, Conn);
command.Parameters.AddWithValue("@SmallBoxCode", smallBoxCode);
Conn.Open();
SqlDataReader reader = command.ExecuteReader();
// 将查询结果显示在datagridview中
DataTable resultDt = new DataTable();
resultDt.Load(reader);
dataGridView1.DataSource = resultDt;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}