在SqlCommands查询中苦苦挣扎让我感到困惑

时间:2016-10-08 20:15:20

标签: c# sql sql-server

我正努力为我的数据库制作测试程序。

我的数据库连接正常工作,我似乎无法使查询正常工作。我不知道如何在列表框或其他内容中查看结果。

这是我到目前为止所做的:

public static string Query1()
{
    List<string[]> returnvalue = new List<string[]>();

    if (DatabaseConnection.OpenConnection())
    {
        try
        {              
            DatabaseConnection.OpenConnection();

            SqlCommand cmd = new SqlCommand("SELECT * FROM UniqueWeapon U, Weapon W WHERE W.WeaponID = U.WeaponIDFK AND U.AllyIDFK IS NOT NULL AND W.Damage > 10 AND W.WeaponType = 'Spell';", DatabaseConnection.connect);
            cmd.Connection = DatabaseConnection.connect;

            SqlDataAdapter dap = new SqlDataAdapter(cmd);

            DataTable tbl = new DataTable();
            dap.Fill(tbl);

            //cmd.CommandText = "SELECT * FROM UniqueWeapon U, Weapon W WHERE W.WeaponID = U.WeaponIDFK AND U.AllyIDFK IS NOT NULL AND W.Damage > 10 AND W.WeaponType = 'Spell';";
            //cmd.ExecuteNonQuery();             

            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                dap.Fill(tbl);
            }
        }
        catch (SqlException e)
        {
            Console.WriteLine("Query Failed: " + e.StackTrace + e.Message.ToString());
            return "returnvalue";
        }
        finally
        {
            DatabaseConnection.CloseConnection();
        }
    }

    return "returnvalue";
}

任何反馈或帮助都会生病!

谢谢!

1 个答案:

答案 0 :(得分:1)

您的代码中存在一些问题:

1)使用方法DatabaseConnection.OpenConnection()

if (DatabaseConnection.OpenConnection()) // means it's true with Connection to db
{
    try
    {              
        DatabaseConnection.OpenConnection(); // why you open it again
       .......

2)你返回虚拟字符串,并且你已经创建了填充了数据但在方法范围之外不可见的本地DataTable

3)你填充数据表两次

4)您没有使用Debug跟踪结果以了解问题所在

修改您的代码,如下所示: 在您的代码中阅读我的评论。我创建了DatabaseConnection类来测试提供的代码并且它正在工作。

 public static DataTable Query1()
    {
       // why this variable returnvalue, you don't fill it
        List<string[]> returnvalue = new List<string[]>(); 

        //check the connection with database by using DatabaseConnection.OpenConnection() only once, and using the property Connect

        if (DatabaseConnection.connect.State == ConnectionState.Closed)
            DatabaseConnection.OpenConnection();

        DataTable tbl = new DataTable(); //use it as a return value

        try
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM UniqueWeapon U, Weapon W WHERE W.WeaponID = U.WeaponIDFK AND U.AllyIDFK IS NOT NULL AND W.Damage > 10 AND W.WeaponType = 'Spell';", DatabaseConnection.connect);

            cmd.Connection = DatabaseConnection.connect; //connect is SQlConnection

            SqlDataAdapter dap = new SqlDataAdapter(cmd);

            //DataTable tbl = new DataTable(); //use it as a return value
            dap.Fill(tbl);
            Debug.WriteLine(tbl.Rows.Count);
            //cmd.CommandText = "SELECT * FROM UniqueWeapon U, Weapon W WHERE W.WeaponID = U.WeaponIDFK AND U.AllyIDFK IS NOT NULL AND W.Damage > 10 AND W.WeaponType = 'Spell';";
            //cmd.ExecuteNonQuery();             

            //why that code, you already filled the table using SqlDataAdapter

            //SqlDataReader reader = cmd.ExecuteReader();

            //while (reader.Read())
            //{
            //    dap.Fill(tbl);
            //}

            // but if you want to fill datatable from Datareader use: tbl.Load(reader)
        }
        catch (SqlException e)
        {
            Console.WriteLine("Query Failed: " + e.StackTrace + e.Message.ToString());
            //return "returnvalue";
        }
        finally
        {
            DatabaseConnection.CloseConnection();
        }


        //return "returnvalue";
        return tbl;
    }