locked
Read sql table and insert result in a var RRS feed

  • Question

  • I have a table with records, I need to read all the records of certain fields and if those fields are empty assign a variable to the value zero and if it contains some value assign the result to a variable. I have the code in VB but I need the code in c#  with sql db 

    CODE IN VB 

    Public Sub formT1()
    Dim SQL As String
    Dim base As Database
    Set base = CurrentDb
    Dim rst As Recordset
    
    Dim code_em As String
    
         
    
    Set rst = base.OpenRecordset("SELECT * FROM [MEmp] WHERE [MEmp].[] = 2011")
    
    Do While rst.EOF = False
    
        
        If IsNull(rst.Fields("CODE")) Then
            code_em = "0"
        Else
            code_em = rst.Fields("CODE")
        End If
        
        
          
            
        
        Call Regla_118(code_em)
          
        
        SQL = "INSERT INTO VALIDATE(REGLA_118) VALUES" _
                & "('" & Regla118 & "')"
            
        DoCmd.RunSQL SQL
     
     rst.MoveNext
    
    Loop
    
    base.Close
    Set base = Nothing
    End Sub

    CODE I have tried in C#

    public static void ValidarT1(string C_E)
            {
                string constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                   
    
                    string query = "SELECT * FROM Empresas_Relacionadas";
                  
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        cmd.Connection = con;
                        con.Open();
                        using (SqlDataReader sdr = cmd.ExecuteReader())
                        {
                            while (sdr.Read())
                            {
    
                                if (sdr.get)
                                {
                                    C_E = "0";
                                }
                                else
                                {
                                    //C_E = Value column 
    
                                }
    
    
                                ReglaController.Regla_1(C_E);
    
                                // INSERT TO Table 
    
    
                            }
                        }
                        con.Close();
                    }
                }
            }

    Tuesday, October 23, 2018 3:58 PM

All replies

  • Perhaps something like this

    public class Operations
    {
        public static void ValadarT1(string C_E)
        {
            string constr = "";
            string query = "SELECT * FROM Empresas_Relacionadas";
    
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(query) { Connection = con })
                {
                    con.Open();
                    var reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        C_E = string.IsNullOrWhiteSpace(reader.GetString(0)) ? "Some value" : reader.GetString(0);
                    }
                }
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, October 23, 2018 8:18 PM