数据库连接字符串 [英] Database Connection String

查看:108
本文介绍了数据库连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚安全晚。

    我是编码新手,并且一直试图找到以下情况的答案:

     I am brand new to coding and have been trying to track down an answer to the following situation:

我正在构建一个使用Access数据库作为部分主干的WPF程序。该程序要求用户选择一个数据库文件以与其程序实例一起使用。一旦用户选择了相应的文件,完整路径就会显示在表单上的文本
框中。我想要做的是使用在用户选择数据库文件后在文本框中填充的文件路径将连接字符串写入数据库。我一直在寻找关于如何做
的正确语法几天没有成功。我已将相关的代码段放在下面。

I am building a WPF program that uses an Access Database as a partial backbone. The program requires the user to pick a database file to use with their instance of the program. Once the user chooses the appropriate file the complete path shows up in a text box on the form. What I am trying to do is write the connection string to the database using the file path that is populated in the text box as the data source after the user chooses their database file. I have been searching for proper syntax on how to do this for a few days with no success. I've put the pertinent code snippet below.

  //数据库连接变量

     ;    OleDbConnection dbTest;

        OleDbCommand cmd1;

        string strcon = @" Provider = Microsoft.ace.oleDB.12.0; data source = J:\ Average Joes'Theatrical Automation\Fluid Canvas UI \ WPF Fluid Canvas \ Test Database.accdb;" ;;

      

       

        //数据库加载按钮

        private void Button_Click(object sender,RoutedEventArgs e)

        {

            Microsoft.Win32.OpenFileDialog dbLoad = new Microsoft.Win32.OpenFileDialog();

            dbLoad.DefaultExt =" .accdb" ;;

            dbLoad.Filter =" Access Database(.accdb)| * .accdb";

推荐答案

你好Joe Fasciano,

Hi Joe Fasciano,

感谢您在此发帖,

对于您的问题,这里是从Access数据库读取数据并在datagridView中显示的代码。

For your question, here is the code to read data from Access database and show in datagridView.

  private void Button1_Click(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToAddRows = false;
            string strDSN = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" + textBox1.Text;

            string strSQL = "SELECT * FROM Table1";
            // create Objects of ADOConnection and ADOCommand  
            OleDbConnection myConn = new OleDbConnection(strDSN);
            OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL, myConn);
            //myConn.Open();
            DataSet dtSet = new DataSet();
            myCmd.Fill(dtSet, "Table1");
            DataTable dTable = dtSet.Tables[0];
            dataGridView1.DataSource = dtSet.Tables["Table1"].DefaultView;
            myConn.Close();


        }

如果您可以在TextBox1中获取访问数据库路径和输入,则可以在datagridView中显示该表。

If you could get the access database path and input in TextBox1, you could show the table in datagridView.

最好的问候,

Wendy


这篇关于数据库连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆