我想知道是否有可能从OleDbdataReader
检索特定值。
例如,我从有100行10列的excel表中导入数据。然后我想让阅读器从指定行的所有列中获取值。假设我有一个文本框,将随机数(85)放入其中,阅读器在第一列中找到这个数字并相应地读取所有列的值。
是的,如果可能的话,你能提供一些指导如何做到这一点吗?
到目前为止,我只设法让读者读取所有的值。
主要思想是通过改变文本框中的值,读者将应用不同的值集到方法中。链接到excel表格中的图片,使其更清楚:
https://imagizer.imageshack.us/v2/505x318q90/743/YIVGVf.jpg private void button1_Click(object sender, EventArgs e)
{
Model ThisModel = new Model();
Beam ThisBeam = new Beam();
OleDbConnection conn = new OleDbConnection();
const string excelConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=@1;";
const string connStringExtras = @"Extended Properties=""Excel 8.0;HDR=Yes;Imex=1""";
conn.ConnectionString = excelConnString.Replace("@1", "C:\Users\Andrejs\Desktop\API2.xls;") + connStringExtras;
try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT A, B, C, D, E, F "+
"FROM [SHEET1$]", conn);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
double X = (double)reader.GetValue(1);
double Y = (double)reader.GetValue(2);
CreatePadFooting(X, Y);
}
}
catch
{
}
ThisModel.CommitChanges();
}
private static void CreatePadFooting(double X, double Y)
{
Beam PadFooting1 = new Beam();
PadFooting1.Name = "FOOTING";
PadFooting1.Profile.ProfileString = "200*200";
PadFooting1.Material.MaterialString = "K30-2";
PadFooting1.Class = "8";
PadFooting1.StartPoint.X = X;
PadFooting1.StartPoint.Y = Y;
PadFooting1.StartPoint.Z = 0.0;
PadFooting1.EndPoint.X = X;
PadFooting1.EndPoint.Y = Y;
PadFooting1.EndPoint.Z = -500.0;
PadFooting1.Position.Rotation = Position.RotationEnum.FRONT;
PadFooting1.Position.Plane = Position.PlaneEnum.MIDDLE;
PadFooting1.Position.Depth = Position.DepthEnum.MIDDLE;
PadFooting1.Insert();
}
}
}
您需要一个If
语句来比较第一列的值,然后从Reader中读取。像这样插入一个If语句:
double randomValue = Convert.ToDouble(txtBoxRandomValue.Text); //get value to match and convert to double
try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT A, B, C, D, E, F "+
"FROM [SHEET1$]", conn);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if (randomValue == Convert.ToDouble(reader.GetValue(0))) //checking here the first column
{
double X = (double)reader.GetValue(1);
double Y = (double)reader.GetValue(2);
CreatePadFooting(X, Y);
}
}
}
catch
{
}