2017-08-26 43 views
0

我试图比较每次用户登录时更新的数据库中的值。当我使用给定的代码执行查询时,什么都不会发生。然而,如果我给它一个值(说明尝试> 10),它会在哪里出错?将SQL值整数

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; 
    } 
} 

回答

1

如果列Attempt是一个整数,你不必通过比较值到它的字符串(从事实Attempt < 10运行为明显)。所以,您的查询应该是这样的:

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

我会建议你创建一个查询变量,然后运行在SQL查询手动看看是什么错误调试在未来这样的错误。你可以做这样的事情:

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

什么也没有发生 - 没有足够的信息,正确的答案。如果实际上没有任何反应,那么删除所有代码周围的块,并再次运行应用程序。那么如果出现错误,您将以Exception的形式获得有关错误信息的非常有用的信息。

但是,问题似乎是您将错误的数据传递给数据库查询。
总是使用SqlParameter将动态数据传递给查询。 SqlParameter的类型可以设置为要对其进行操作的列的对应类型。另外SqlParameter将保护你免受sql注入。

使用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 
    }   
}