c#
  • c#-4.0
  • 2012-06-11 90 views 0 likes 
    0

    我有一個php code.here是代碼,我要把它翻譯成.NET,但在某些時候我遇到了一些麻煩。執行讀卡器沒有響應

    function processInput($conn, $MessageArray, $mobilenumber, $date, $odd) 
    { 
    
    $strSQLUSER="SELECT * FROM tbl_tiduser WHERE username='".addslashes($MessageArray[0])."' AND stat!='1' AND stat!='4'"; 
    
    $result_user=odbc_exec($conn,$strSQLUSER) or die("Could not connect to database"); 
    

    這裏是轉換後的.NET代碼

    public class ProcessInput 
    { 
        private string msg_arr; 
        private string MooseSeenInput(string MobileNo,string Date,string odd,params Array[] msg_arr) 
        { 
    
    
    SqlCommand com = new SqlCommand("SELECT * FROM tbl_tiduser WHERE [email protected] AND stat!='1' AND stat!='4'", mycon); 
            com.Parameters.AddWithValue("@username",username); 
            using (SqlDataReader reader = com.ExecuteReader()) 
    
        // whats the next part need to come here ??? 
    
    } 
    

    這incomplete.i'm不會編譯....

    +0

    你會遇到什麼樣的麻煩? –

    回答

    3
    private static void ReadOrderData(string connectionString) 
    { 
        string queryString = 
         "SELECT OrderID, CustomerID FROM dbo.Orders;"; 
    
        using (SqlConnection connection = 
           new SqlConnection(connectionString)) 
        { 
         SqlCommand command = 
          new SqlCommand(queryString, connection); 
         connection.Open(); 
    
         SqlDataReader reader = command.ExecuteReader(); 
    
         // Call Read before accessing data. 
         while (reader.Read()) 
         { 
          Console.WriteLine(String.Format("{0}, {1}", 
           reader[0], reader[1])); 
         } 
    
         // Call Close when done reading. 
         reader.Close(); 
        } 
    } 
    

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

    +0

    OP是新手,如果你能解釋一下就更好了。 – Damith

    1

    我會使用類似這樣的方式獲取您之後的列:

    string username = null; 
    using (SqlDataReader reader = com.ExecuteReader()) { 
        if (reader.read()) { 
         username = (string)reader["mydbcolumnname"]; 
        } 
        reader.Close(); 
    } 
    

    請注意,如果你想拉的所有結果行(而不是通過他們步進),那麼你通常使用的SqlDataAdapter 填充DataSet的(而不是閱讀器),如:

    string username; 
    using (SqlDataAdapter adapter = new SqlDataAdapter(com)) 
    { 
        using (DataSet ds) 
        { 
         adapter.Fill(ds); 
         username = (string)ds.Tables[0].Rows[0]["mycolumnname"]; 
        } 
    } 
    
    +0

    之後需要使用executenonquery(); ? – Learner

    1

    我很容易;我會寫一個class,反映我讀的記錄,即

    public class User { 
        public int Id {get;set;} 
        public string Name {get;set;} 
    } 
    

    ,並使用"dapper"

    var user = myCon.Query<User>(
        "SELECT * FROM tbl_tiduser WHERE [email protected] AND stat not in ('1','4')", 
        new {username}).SingleOrDefault(); 
    
    if(user == null) { /* not found, presumably throw an exception */ } 
    
    string name = user.Name; // etc 
    

    然後,你不需要使用命令,讀卡器,參數等亂(見如何將username乾淨地變成db參數?)。

    +0

    在這裏我看到var是這個javascript?要麼 ?? – Learner

    +0

    @Learner no,這就是C#3.意思是相同的用戶用戶= myCon.Query (等等等等等等)' –

    +0

    我使用vs 2010與框架4.0是否有效? – Learner

    相關問題