读取sql表并在var中插入结果 [英] Read sql table and insert result in a var

查看:84
本文介绍了读取sql表并在var中插入结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含记录的表,我需要读取某些字段的所有记录,如果这些字段为空,则将变量赋值为零,如果包含某个值,则将结果赋值给变量。我有VB中的代码,但我需要c#中的代码 
with sql db 


VB中的代码 

 Public Sub formT1()
Dim SQL As String
Dim base As Database
Set base = CurrentDb
Dim rst As Recordset

Dim code_em As String



设置rst = base.OpenRecordset(" SELECT * FROM [MEmp] WHERE [MEmp]。[] = 2011")

Do While rst.EOF = False


如果IsNull(rst.Fields(" CODE")))则
code_em =" 0"
Else
code_em = rst.Fields(" CODE")
End if





调用Regla_118(code_em)


SQL =" INSERT INTO VALIDATE(REGLA_118)VALUES" _
& "('"& Regla118&"')"

DoCmd.RunSQL SQL

rst.MoveNext

循环

base.Close
设置基数=没有
结束子

我在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" ;;

使用(SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
使用(SqlDataReader sdr = cmd.ExecuteReader())
{
while(sdr.Read())
{

if(sdr.get) )
{
C_E =" 0" ;;
}
其他
{
// C_E =价值栏

}


ReglaController.Regla_1( C_E);

// INSERT TO Table


}
}
con.Close();
}
}
}


解决方案

也许是这样的事情

公共类运营
{
public static void ValadarT1(string C_E)
{
string constr ="" ;;
string query =" SELECT * FROM Empresas_Relacionadas" ;;

使用(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))? "一些价值" :reader.GetString(0);
}
}
}
}
}


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

解决方案

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


这篇关于读取sql表并在var中插入结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆