简体   繁体   English

SQL值转换为整数

[英]SQL Values to integers

I'm trying to compare values in a database that are updated every time a user logs in. When I execute a query with the given code nothing happens. 我正在尝试比较每次用户登录时都会更新的数据库中的值。当我使用给定的代码执行查询时,什么都没有发生。 However if I give it a value of say (where Attempt >10) it works where am I going wrong? 但是,如果我给它说的值(尝试> 10),那么它在哪里出错了?

private void User_Tick(object sender, EventArgs e)
{
    SqlConnection con13 = new SqlConnection("Data Source = *** ")

    SqlDataAdapter SDA2 = new SqlDataAdapter("SELECT [User],[Login],[number1],[number2],[number3],[Alertcount] FROM Users.dbo.[Email] where [Alertcount] = 1 and [Alertcount] !=2", con13);

    DataTable Users = new DataTable();
    DataTable DATA2 = new DataTable();

    SDA2.Fill(DATA2);

    dataGridView2.DataSource = DATA2;

    foreach (DataGridViewRow dr in dataGridView2.Rows)
    {
        string col2 = 1.Cells["User"].Value.ToString();
        string col1 = 1.Cells["Login"].Value.ToString();
        string col3 = 1.Cells["number1"].Value.ToString();
        string col4 = 1.Cells["number2"].Value.ToString();
        string col5 = 1.Cells["number3"].Value.ToString();
        string col6 = 1.Cells["Alertcount"].Value.ToString();

        var mine = Convert.ToInt32(col3);
        var mine2 = Convert.ToInt32(col5);

        SqlConnection CON2 = new SqlConnection("Data Source = ***")
        CON2.Open();

        SqlDataAdapter SDA = new SqlDataAdapter("SELECT [User],[Login],[Attempt] FROM User.dbo.Actions where  [Attempt] >  '"+mine+ "' and [Attempt] < '" + mine2 + "'", CON2);

        DataTable DATA = new DataTable();
        SDA.Fill(DATA);

        dataGridView1.DataSource = DATA;
    }
}

If column Attempt is an integer (as evident from the fact that Attempt < 10 runs), you need not pass comparison values to it in string. 如果Attempt列是整数(从Attempt < 10运行这一事实就可以看出),则无需将比较值以字符串形式传递给它。 So your query should be like this: 因此,您的查询应如下所示:

SqlDataAdapter SDA = new SqlDataAdapter("SELECT [User],[Login],[Attempt] FROM User.dbo.Actions where  [Attempt] >  "+mine+ " and [Attempt] < " + mine2 , CON2);

I would suggest you to debug such errors in future by creating a query variable and then running the query in SQL manually to see what the error is. 我建议您以后通过创建查询变量来调试此类错误,然后在SQL中手动运行查询以查看错误是什么。 You could do something like this: 您可以执行以下操作:

var query = "SELECT [User],[Login],[Attempt] FROM User.dbo.Actions where  [Attempt] >  "+mine+ " and [Attempt] < " + mine2 ;
SqlDataAdapter SDA = new SqlDataAdapter(query , CON2);

nothing happens - not enough information for correct answer. 没有任何反应 -没有足够的信息来提供正确答案。 If actually nothing happens, then remove all try catch blocks you have around code and run application again. 如果实际上什么也没发生,那么请删除代码周围的所有try catch块,然后再次运行应用程序。 Then if something wrong you will get very useful information about what gone wrong in the form of Exception . 然后,如果出现问题,您将以Exception的形式获得有关发生问题的非常有用的信息。

However, problem seems is that you passing wrong data to database query. 但是,问题似乎是您将错误的数据传递给数据库查询。
Always use SqlParameter for passing dynamic data to the query. 始终使用SqlParameter将动态数据传递给查询。 SqlParameter have type which you can set to correspondent type of column you want operate on. SqlParameter具有可以设置为要操作的列的对应类型的类型。 Also SqlParameter will protect you from sql injection. SqlParameter还将保护您免受sql注入。

Use using for disposable objects when ever it possible (read "always") 尽可能using一次性物品(常读)

var emailQuery = 
    @"SELECT [User] ,[Login] ,[number1] ,[number2] ,[number3] ,[Alertcount] 
    FROM Users.dbo.[Email] 
    WHERE [Alertcount] = 1 
        AND [Alertcount] !=2"; // Useless condition, because Alertcount already = 1

using(var connection2 = new SqlConnection("Data Source = *** "))
using(var adapter2 = new SqlDataAdapter(emailQuery, connection1))
{
    var data2 = new DataTable();
    adapter2.Fill(data2);
    dataGridView2.DataSource = data2;
}

var actionsQuery = 
    @"SELECT [User] ,[Login] ,[Attempt] 
    FROM User.dbo.Actions 
    WHERE Attempt > @Mine AND Attempt < @Mine2";
foreach (var row in dataGridView2.Rows)
{
    var mine = (int)row.Cells["number1"].Value; // it is already integer, just cast it
    var mine2 = (int)row.Cells["number3"].Value;

    using(var connection1 = new SqlConnection("Data Source = *** "))
    using(var adapter1 = new SqlDataAdapter(actionsQuery, connection1))
    {
        var parameters = new[]
        {
            new SqlParameter
            {
                ParameterName = "@Mine",
                SqlDbType = SqlDbType.Int,
                Value = mine
            },
            new SqlParameter
            {
                ParameterName = "@Mine2",
                SqlDbType = SqlDbType.Int,
                Value = mine2
            }
        };
        adapter1.SelectCommand.Parameters.AddRange(parameters);

        var data1 = new DataTable();
        adapter.Fill(data1);
        dataGridView1.DataSource = data1
    }        
}

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

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