-1

我正在尝试编写一个过程来检查 SQL 表中是否有一些数据。

然后我想根据是否存在做一些事情。

所以,我有这个连接到数据库的代码,它正在选择我想从表中获取的数据。

public List<MedidasAdoptar> leerMedidasAdoptar()
    {

        DataSet Datos = new DataSet();
        string connectionString = "Initial Catalog=MyDatabase;Data Source=MyServer;Integrated Security=false;User ID=SQLUser;Password=SQLPass;";
        string selectCommand =
            "SELECT [Número medida], [Cód_ Cliente], [Nombre Cliente], [Cód_ obra], [Nombre obra], Tipo, [Cód_ Medida], [Descripción Medida], [Respuesta Medida] FROM [MyDatabase$My table] " +
            "WHERE [Cód_ Cliente] = "+codigoCliente+" AND [Cód_ obra] = "+codigoObra;
        using (var MyConnection = new SqlConnection(connectionString))
        using (var MyDataAdapter = new SqlDataAdapter(selectCommand, MyConnection))
        {
            MyDataAdapter.SelectCommand.CommandType = CommandType.Text;
            MyDataAdapter.Fill(Datos);
            MyDataAdapter.Dispose();
            MyConnection.Close();
        }
        int i;

        //Right here (If it is possible) I want to determine if there is some data in table 

        try
        {
          //...doing some stuff
        } catch (SqlException e)
        {


            string msg = "";
            System.Console.WriteLine(msg);
            return new List<MedidasAdoptar>();
        }
4

1 回答 1

1

像这样的东西应该工作

if (Datos.Tables[0].Rows.Count() > 0)
于 2012-07-12T16:08:31.413 回答