【发布时间】:2015-09-17 19:06:18
【问题描述】:
大家好,我需要一些帮助,在 C# 中使用 WPF 应用程序导出 SQL 表。我需要它在应用程序启动后立即运行。下面是示例代码:
public MainWindow()
{
InitializeComponent();
testie();
}
这是我试图在程序开始时初始化的主窗口。 这是我的方法:
private void testie(string[] args)
{
string connectionString = "Data Source=sqlserver;" + "Initial Catalog=database;" + "Integrated Security=True;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
}
catch (System.Data.SqlClient.SqlException ex)
{
// handle
return;
}
string selectCommandText = "SELECT * FROM tablet";
using (SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, connection))
{
using (DataTable table = new DataTable("tablet"))
{
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(@"C:\Users\desktop\test666.txt", commaDelimitedText.ToString());
}
}
}
}
我收到一个错误“方法 'testie' 没有重载需要 0 个参数。” 如果我改变方法:
private void testie(string[] args)
到:
private void testie(object sender, EventArgs e)
我遇到了同样的问题。 任何帮助表示赞赏,在此先感谢。
【问题讨论】: