Select 查询无效

Select Query not working

我制作了一个 class 数据库并在我的表单中使用它,但是我的 select 查询不工作并且 datagridview 仍然是空的.. 这是我的 class..

class ConnectDB
{
    private SqlConnection xconn;

    public ConnectDB()
    {
        xconn = new SqlConnection(new StreamReader("ConnectionDB.txt").ReadLine());
    }

    public void DMLOperations(string Query) //Execute Queries e.g Insert | Update Delete
    {
        xconn.Open();
        new SqlCommand(Query,xconn).ExecuteNonQuery();
        xconn.Close();
    }
    public DataTable GetData(string SelectQuery)
    {
        DataTable xdata = new DataTable();
        new SqlDataAdapter(SelectQuery,xconn);
        return xdata;
    }
    public void CloseDB()
    {
        xconn.Dispose();
    }
    }

& 这个我的 method 不工作

 private void btnSearch_Click(object sender, EventArgs e)
    {
        string batch = Batch.Text;
        xDB.GetData("Select * from Students Where batch ='" + batch + "' ");
        dataGridView1.DataSource = xDB.GetData("Select * from Students Where batch ='"+batch+"' ");
        Batch.Clear();
        Batch.Focus();
    }

您忘记填写 table。

public DataTable GetData(string SelectQuery)
{
    using(var da = new SqlDataAdapter(SelectQuery,xconn))
    {
        DataTable xdata = new DataTable();
        da.Fill(xdata);
        return xdata;
    }
}

首先不要用ConnectionConnection pooling是你的朋友!

Connection pooling reduces the number of times that new connections must be opened. The pooler maintains ownership of the physical connection. It manages connections by keeping alive a set of active connections for each given connection configuration. Whenever a user calls Open on a connection, the pooler looks for an available connection in the pool. If a pooled connection is available, it returns it to the caller instead of opening a new connection. When the application calls Close on the connection, the pooler returns it to the pooled set of active connections instead of closing it. Once the connection is returned to the pool, it is ready to be reused on the next Open call.

这是关于 Connection pooling

的完整文章

如何构建数据访问层:

public class SqlManager
{

    public static string ConnectionString
    {
        get
        {
            return ConfigurationManager.ConnectionStrings["DevConnString"].ConnectionString;
        }
    }


    public static SqlConnection GetSqlConnection(SqlCommand cmd)
    {
        if (cmd.Connection == null)
        {
            SqlConnection conn = new SqlConnection(ConnectionString);

            conn.Open();

            cmd.Connection = conn;

            return conn;
        }

        return cmd.Connection; 
    }

    public static int ExecuteNonQuery(SqlCommand cmd)
    {
        SqlConnection conn = GetSqlConnection(cmd);

        try
        {
            return cmd.ExecuteNonQuery();
        }
        catch
        {
            throw;
        }
        finally
        {
            conn.Close();
        }
    }

    public static object ExecuteScalar(SqlCommand cmd)
    {

        SqlConnection conn = GetSqlConnection(cmd);

        try
        {
            return cmd.ExecuteScalar();
        }
        catch
        {
            throw;
        }
        finally
        {
            conn.Close();
        }
    }

    public static DataSet GetDataSet(SqlCommand cmd)
    {
        return GetDataSet(cmd, "Table");
    }

    public static DataSet GetDataSet(SqlCommand cmd, string defaultTable)
    {
        SqlConnection conn = GetSqlConnection(cmd);

        try
        {
            DataSet resultDst = new DataSet();

            using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
            {
                adapter.Fill(resultDst, defaultTable);
            }

            return resultDst;
        }
        catch
        {
            throw;
        }
        finally
        {
            conn.Close();
        }
    }


    public static DataRow GetDataRow(SqlCommand cmd)
    {
        return GetDataRow(cmd, "Table");
    }

    public static DataRow GetDataRow(SqlCommand cmd, string defaultTable)
    {
        SqlConnection conn = GetSqlConnection(cmd);

        try
        {
            DataSet resultDst = new DataSet();

            using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
            {
                adapter.Fill(resultDst, defaultTable);
            }

            if (resultDst.Tables.Count > 0 && resultDst.Tables[0].Rows.Count > 0)
            {
                return resultDst.Tables[0].Rows[0];
            }
            else
            {
                return null;
            }
        }
        catch
        {
            throw;
        }
        finally
        {
            conn.Close();
        }
    }
}

在这个简单的数据访问层中,我们有 4 个主要方法:ExecuteNonQueryExecuteScallarGetDataSetGetDataRow。要与他们合作,您需要给他们 SqlCommand 作为参数。

第三次使用 SqlCommand 参数这将有助于 SQL 服务器缓存查询并防止 Sql 注入。如果您不知道什么是 Sql 注入,请查看这篇文章:SqlInjection

最后,您的代码将如何使用 DataAccessLayer:

private void btnSearch_Click(object sender, EventArgs e)
{
    string batch = Batch.Text;

    SqlCommand cmd = new SqlCommand(@"
                           SELECT
                               *
                           FROM
                               Student
                           WHERE
                               Batch=@Batch");


    DataSet dst = SqlManager.GetDataSet(cmd);

    dataGridView1.DataSource = dst.Tables[0];

    Batch.Clear();
    Batch.Focus();
}

请注意,在项目的 GUI 部分执行数据访问不是好的做法。更好的做法是拥有业务层,您将在其中执行数据访问层。