通过文本框连接到SQL DB

时间:2016-01-30 19:49:14

标签: c# sql sql-server

所以我尝试使用粘贴到文本框中的连接字符串,以便在单击连接按钮时连接到数据库。我有以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SQLTool
{
    public partial class SQLTool : Form
    {
        public SQLTool()
        {
            InitializeComponent();

        }
        public static SqlConnection myConnection = null;

        public void Connection()
        {
            myConnection = new SqlConnection(DBConnectionBox.Text);
            //myConnection.Open();
        }


        private void DBConnectBtn_Click(object sender, EventArgs e)
        {
            SqlConnection myConnection = new SqlConnection(DBConnectionBox.Text.ToString());
            myConnection.ConnectionString = "Data Source=ServerName;" + "Initial Catalog=DatabaseName;" + "User ID=UserName;" + "Password=Password";
            myConnection.Open();

            if (myConnection !=null && myConnection.State == ConnectionState.Closed)
            {
                MessageBox.Show("SUCCESS!!");
            }


        }




    }
}

我添加了我的表单图片。组合框我将添加来自我连接的数据库的数据。因此,对此的任何帮助也将受到赞赏。 Form Picture

也许有人可以指出我正确的方向。 if语句只是用于测试我是否连接到数据库。这是我第一次尝试这样的事情,所以我有点失落。任何帮助将不胜感激。

更新:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SQLTool
{
    public partial class SQLTool : Form
    {
        public SQLTool()
        {
            InitializeComponent();

        }
        public static SqlConnection myConnection = null;




        private void DBConnectBtn_Click(object sender, EventArgs e)
        {

            try
            {
                var myConnection = new SqlConnection(DBConnectionBox.Text);
                myConnection.Open();
                if (myConnection.State == ConnectionState.Open)
                {
                    var sqlCmd = new SqlCommand("SELECT * FROM Menu", myConnection);
                    var sqlReader = sqlCmd.ExecuteReader();

                    while (sqlReader.Read())
                        ClientComboBox.Items.Add(sqlReader["Name"].ToString());

                    //MessageBox.Show("SUCCESS!!");


                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Failed to connect. Message {ex.Message}");
            }


        }


    }
}

唯一要做的就是使用运行的SQL查询填充datagridview,这取决于我在组合框中选择的内容。

好的,新问题:

private void ClientComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {

        try
        {
            var myConnection = new SqlConnection(DBConnectionBox.Text);
            myConnection.Open();
            if  (myConnection.State == ConnectionState.Open)
            {
                var Cmd = new SqlCommand("SELECT * FROM Menu WHERE Name ='" + ClientComboBox.Text + "';");
                var Reader = Cmd.ExecuteReader();

                while(Reader.Read())
                {
                    SqlDataAdapter sqlDataAdap = new SqlDataAdapter(Cmd);

                    DataSet dtRecord = new DataSet();
                    sqlDataAdap.Fill(dtRecord);
                    ClientInfoDGV.DataSource = dtRecord;

                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

当我点击组合框项目时,会在datagridview(ClientInfoDGV)中填充一些内容。它直接捕获。

1 个答案:

答案 0 :(得分:1)

您不应重新初始化ConnectionString对象的myConnection属性,因为它已经初始化。

另请注意,您应检查myConnection.State == ConnectionState.Open以确定连接的打开位置。

您可以尝试以下

public static SqlConnection myConnection = null;
private void DBConnectBtn_Click(object sender, EventArgs e)
{
  try
  {
    var myConnection = new SqlConnection(DBConnectionBox.Text);
    myConnection.Open();
    if (myConnection.State == ConnectionState.Open)
      MessageBox.Show("SUCCESS!!");
  }
  catch (Exception ex)
  { MessageBox.Show($"Failed to connect. Message {ex.Message}"); }
}