我在sql server 2008中有一个名为 - "dbo.COM_ConnectionLogRfmDevices"
的表。
该表包含7列,其名称如下: -
CL_ID,CL_UnitNumber,CL_RemoteIP,CL_RemotePort,CL_RecDateTime,CL_GPRS,CL_COM.
CL_UnitNumber
包含单位数,如下所示: -
352964054838728
352964054868972
352964054839296
352964054868881
我想存储仅列2的内容,即CL_UnitNumber value in string[] array
。
连接字符串: -
string _ConnectionString = "Data Source=192.168.1.60;" +
"Initial Catalog=OLTP_MTEL_DEVICES; User ID=sa; Password=pass,123;";
答案 0 :(得分:1)
如果你需要动态调整数组大小,我会改用List ...
List<string> unitNumbers = new List<string>();
using (SqlConnection con = new SqlConnection(_ConnectionString))
{
con.Open();
using (SqlCommand command = new SqlCommand("SELECT CL_UnitNumber FROM COM_ConnectionLogRfmDevices", con))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
unitNumbers.Add(reader.GetInt32(0)); // Or maybe reader.GetString(0)
}
}
}
答案 1 :(得分:0)
查看此帖子。将实体框架放入您的应用程序,您可以连接到您的数据库并在几分钟内提取您的数据。非常无痛。
答案 2 :(得分:0)
由于您提到了SQL,我想您希望使用原始SQL命令来处理它。 像下面这样的东西应该让你开始。
string[] allRecords = null;
string sql = @"SELECT CL_UnitNumber
FROM some table";
using (var command = new SqlCommand(sql, connection))
{
con.Open();
using (var reader = command.ExecuteReader())
{
var list = new List<string>();
while (reader.Read())
list.Add(reader.GetString(0));
allRecords = list.ToArray();
}
}
答案 3 :(得分:0)
var arr = new List<string>();
var connectionString= "YOUR CONNECTION";
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlCommand command = new SqlCommand("SELECT CL_UnitNumber FROM COM_ConnectionLogRfmDevices", con))
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
arr.Add(reader["CL_UnitNumber"] != DBNull.Value
? reader["CL_UnitNumber"].ToString()
: "");
}
}
}
return arr.ToArray();