Dav*_*ave 2 c# sql sql-server export
所以我正在寻找一种从SQL Server 2000数据库导出数据并将其写入逗号分隔文本文件的简单方法.它的一个表只有大约1,000行.我是C#的新手所以请原谅我这是一个愚蠢的问题.
这是一项非常简单的任务,但您需要学习SqlClient命名空间以及您可以使用的不同对象.您需要注意的是,对于SQL Server 2000而言,不支持较低的异步方法,因此它们将全部阻塞.
请注意,这是一个非常粗略的例子,我没有对此进行测试,但这是一种通用的方法.
string connectionString = "<yourconnectionstringhere>";
using (SqlConnection connection = new SqlConnection(connectionString)) {
try {
connection.Open();
}
catch (System.Data.SqlClient.SqlException ex) {
// handle
return;
}
string selectCommandText = "SELECT * FROM <yourtable>";
using (SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, connection)) {
using (DataTable table = new DataTable("<yourtable>")) {
adapter.Fill(table);
StringBuilder commaDelimitedText = new StringBuilder();
commaDelimitedText.AppendLine("col1,col2,col3"); // optional if you want column names in first row
foreach (DataRow row in table.Rows) {
string value = string.Format("{0},{1},{2}", row[0], row[1], row[2]); // how you format is up to you (spaces, tabs, delimiter, etc)
commaDelimitedText.AppendLine(value);
}
File.WriteAllText("<pathhere>", commaDelimitedText.ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
您需要查看的一些资源:
我也不确定你的要求是什么,或者你为什么要做这个任务,但也有很多工具可以为你做这个(如果这是一次性的事情),因为这不是一个不寻常的任务.
| 归档时间: |
|
| 查看次数: |
11304 次 |
| 最近记录: |