Asp.net 创建数据字典以从数据库中读取列
我正在创建一个WinForm应用程序,它读取文本文件中某一列的所有记录。我现在需要的是一个数据字典,一旦应用程序运行,在读取文本文件之前,我可以使用它从数据库中读取记录。我需要从数据库中读取特定列,并将其与文本文件匹配。我不知道如何着手创建数据字典。这就是我目前所拥有的 这是为了读取文本文件,该文件工作正常。Asp.net 创建数据字典以从数据库中读取列,asp.net,winforms,Asp.net,Winforms,我正在创建一个WinForm应用程序,它读取文本文件中某一列的所有记录。我现在需要的是一个数据字典,一旦应用程序运行,在读取文本文件之前,我可以使用它从数据库中读取记录。我需要从数据库中读取特定列,并将其与文本文件匹配。我不知道如何着手创建数据字典。这就是我目前所拥有的 这是为了读取文本文件,该文件工作正常。 using (StreamReader file = new StreamReader("C:\\Test1.txt")) {
using (StreamReader file = new StreamReader("C:\\Test1.txt"))
{
string nw = file.ReadLine();
textBox1.Text += nw + "\r\n";
while (!file.EndOfStream)
{
string text = file.ReadLine();
textBox1.Text += text + "\r\n";
string[] split_words = text.Split('|');
int dob = int.Parse(split_words[3]);
public static Dictionary<int, string> dictionary = new Dictionary<int, string>();
这就是我迄今为止创建数据字典的内容。
using (StreamReader file = new StreamReader("C:\\Test1.txt"))
{
string nw = file.ReadLine();
textBox1.Text += nw + "\r\n";
while (!file.EndOfStream)
{
string text = file.ReadLine();
textBox1.Text += text + "\r\n";
string[] split_words = text.Split('|');
int dob = int.Parse(split_words[3]);
public static Dictionary<int, string> dictionary = new Dictionary<int, string>();
publicstaticdictionary=newdictionary();
您可以使用SqlDataReader
。这是一些代码,您只需要修改它以满足您的需要。我为您添加了以下评论:
// declare the SqlDataReader, which is used in
// both the try block and the finally block
SqlDataReader rdr = null;
// Put your connection string here
SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
// create a command object. Your query will go here
SqlCommand cmd = new SqlCommand(
"select * from Customers", conn);
try
{
// open the connection
conn.Open();
// 1. get an instance of the SqlDataReader
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string id = (int)rdr["SomeColumn"];
string name = (string)rdr["SomeOtherColumn"];
dictionary.Add(id, name);
}
}
finally
{
// 3. close the reader
if (rdr != null)
{
rdr.Close();
}
// close the connection
if (conn != null)
{
conn.Close();
}
}
我能够做出一些改变,并使其发挥作用。谢谢