【问题标题】:SQL table data export to txt file on desktopSQL表数据导出到桌面txt文件
【发布时间】: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)

我遇到了同样的问题。 任何帮助表示赞赏,在此先感谢。

【问题讨论】:

    标签: c# sql wpf


    【解决方案1】:
    private void testie()
    

    应该做的伎俩。

    【讨论】:

    • 这下搞定了,哇我觉得傻。问题,使用上面的代码和你的建议,我需要它来 bool 关于如何添加 bool 的任何建议?基本上,需要它运行,随着数据的定期变化更新相同的文本文件。
    猜你喜欢
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 2013-09-10
    相关资源
    最近更新 更多