简体   繁体   English

在XAML应用程序中建立SQL连接-下一步应采取的步骤

[英]Establishing SQL connection in XAML application - what steps to take next

I've been following the tutorial at http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL on connecting my C# application to an SQL database, but I'm not quite clear on how to actually invoke the connection from within my MainWindow. 我一直在遵循http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL上的教程,将C#应用程序连接到SQL数据库,但是我不清楚如何实际从我的MainWindow内调用连接。

I've instantiated the object of the class in my main program, to execute the constructor, but I'm not sure on whether or not my connection is being established, or how to go about doing so. 我已经在我的主程序中实例化了该类的对象以执行构造函数,但是我不确定是否正在建立连接或如何建立连接。 Would really appreciate someone pointing me in the right direction. 非常感谢有人指出我正确的方向。

My code so far: 到目前为止,我的代码:

DbConnect.cs DbConnect.cs

namespace SpeedyRent
{
    internal class DbConnect
    {
        private SqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;

        // Constructor
        public DbConnect()
        {
            Initialize();
        }

        // Initialize values
        private void Initialize()
        {
            server = "localhost";
            database = "rent";
            uid = "root";
            password = "password123";
            string connectionString = "SERVER=" + server + ";" + "DATABASE=" +
                                      database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
            connection = new SqlConnection(connectionString);
        }

        // Open connection to database
        private bool OpenConnection()
        {
            try
            {
                connection.Open();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
        }

        // Close connection
        private bool CloseConnection()
        {
            try
            {
                connection.Close();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
    }

} }

MainWindow.xaml.cs MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        CurrentDateTimeTextBlock.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
        var dbConnectObject = new DbConnect();
    }
}

I believe the documentation you need is here: http://msdn.microsoft.com/en-us/library/system.data.common.dbconnection%28v=vs.110%29.aspx but in order to open a connection you would do dbConnectObject.open(); 我相信您需要的文档在这里: http : //msdn.microsoft.com/zh-cn/library/system.data.common.dbconnection%28v=vs.110%29.aspx,但是为了打开连接,您可以会做dbConnectObject.open(); and then do whatever you need to do and close with dbConnectObject.close(); 然后执行您需要做的所有事情,并使用dbConnectObject.close();关闭dbConnectObject.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM