cmd = New SqlCommand("SELECT ID, Name, Dept from table", con)
dr = cmd.ExecuteReader
Dim sb as new StringBuilder
While dr.Read()
''Let's read line by line and Append it to our StringBuilder
sb.AppendLine(
String.Format("{0} | {1} | {2} | {3}",
dr.item("ID"), dr.item("Name"), dr.item("Dept")))
End While
''Now that we have all data in our StringBuilder, lets put into our file
File.WriteAllLines("D:\test.txt", sb.ToString())
P.S。從一個C#人...請驗證代碼是否正確:)(C#代碼如下)
using (SqlConnection con = new SqlConnection("my Connection String"))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "SELECT ID, Name, Dept from table";
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
StringBuilder sb = new StringBuilder();
while (dr.Read())
// Let's read line by line and Append it to our StringBuilder
sb.AppendLine(
String.Format("{0} | {1} | {2} | {3}",
dr["ID"], dr["Name"], dr["Dept"]));
// Now that we have all data in our StringBuilder, lets put into our file
File.WriteAllLines("D:\test.txt", sb.ToString());
}
}
那麼,什麼是錯誤? – UpTheCreek 2010-02-03 08:14:44
@Sosh ReadAllText需要一個路徑,而不是數據!它讀取一個文件路徑...即使PersonID類似於C:\ myfile.txt,D:中的文件將只包含最後一個讀取文件,因爲WriteAllText會覆蓋它(始終創建一個新文件)!你不能在這裏看到大量的時間嗎? – balexandre 2010-02-03 08:30:25
@balexandre - 我不是那個要求答案的人。也不確定我瞭解你的時間觀點。我的觀點是,如果你發佈有關包含錯誤的問題的問題,**當然也應該發佈錯誤。** – UpTheCreek 2010-02-04 06:56:51