0

このクエリはデータベースで実行されました。

 select COUNT(*) from Patient_Data where  DummyValue = 'Y';

 104

number (104)からこれを取得して、カウントがゼロになったときにdatabaseデータベースからコードにこの番号を取得する必要があります。として保存する必要があります。asp.net with c#disable a buttonretrieveinteger

これらのコード行をc#で試しました

    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("select COUNT(*) as PatientCount from Patient_Data where  DummyValue = 'Y' ", cn))
        {
            try
            {

                cn.Open();

                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    int Patcount;
                    if (rdr.Read())
                    {
                        //Code Required

                    }
                }
            }
            catch (Exception ex)
            {

                // handle errors here
            }

        }


    }
4

4 に答える 4

2

以下のようにエイリアスを使用してカウントを取得します。

select COUNT(*) as PatientCount from Patient_Data where  DummyValue = 'Y';

PatientCount コードで値を読み取ります。

GetInt32()関数を使用してcountasを取得できますint
注: SQL インジェクション攻撃につながるクエリでパラメーター値を渡しているため、パラメーター化された SQL クエリを使用してそれらを回避できます。

サンプルコードは次のとおりです。

private int readPatientData()
        {
            int PatientCount = 0;              
            String strCommand = "select COUNT(*) as PatientCount from Patient_Data where  DummyValue = @MyDummyValue";
            using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString))
            {
                sqlConnection.Open();
                using (SqlCommand sqlcommand=new SqlCommand(strCommand,sqlConnection))
                {
                    sqlcommand.Parameters.Add(new SqlParameter("MyDummyValue", 'Y'));
                    SqlDataReader sqlReader = sqlcommand.ExecuteReader();
                    if (sqlReader.Read())
                        PatientCount = sqlReader.GetInt32(0);
                }
            }
            return PatientCount;
        }
于 2013-11-11T07:07:47.407 に答える
1

これらのコード行で問題を解決しました。

        using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("select COUNT(*) as PatientCount from Patient_Data where  DummyValue = 'Y' ", cn))
        {
            try
            {

                cn.Open();

                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    //int Patcount;
                    if (rdr.Read())
                    {
                        int Patcount = int.Parse(rdr["PatientCount"].ToString());
                        if (Patcount != 0)
                        {
                            Label3.Visible = true;
                            Label3.Text = "You have already have "+Patcount+" dummy records,Please update those records by clicking Update Dummy Records Link.";
                            btnSkipSubmit.Visible = false;
                        }
                        //Code Required


                    }
                }
            }
            catch (Exception ex)
            {

                // handle errors here
            }

        }


    }
于 2013-11-11T08:35:01.390 に答える
0

私の知る限り、これを行うには3つの方法があります。

  1. COUNTを使用すると、次のようにこれを行うことができます。

    DummyValue = 'Y' の場合、Patient_Data から RowCount として COUNT(*) を選択します。

  2. ROW_NUMBERを使用すると、次のようにこれを行うことができます。

    ROW_NUMBER() OVER (ORDER BY Patient_Data.ID DESC) AS RowNumber from Patient_Data where DummyValue = 'Y'; を選択します。

  3. 別の方法がありますが、その方法は行数を取得することだけであり、私によれば、SQLサーバーで行数を取得する最速の方法です。

    SELECT Total_Rows= SUM(st.row_count) FROM sys.dm_db_partition_stats st WHERE object_name(object_id) = 'Patient_Data' AND (index_id < 2)

それで全部です

于 2013-11-11T07:18:52.530 に答える
0

問題のコード変更に基づく更新

rdr.GetInt32(3);コードの代わりに書くことができcode requiredます。

前の回答

コマンドの実行中にExecuteScalarメソッドを使用する必要があります。msdn の例を次に示します。

static public int AddProductCategory(string newName, string connString)
{
    Int32 newProdID = 0;
    文字列 SQL =
        "INSERT INTO Production.ProductCategory (名前) 値 (@名前);"
        + "SELECT CAST(scope_identity() AS int)";
    using (SqlConnection conn = new SqlConnection(connString))
    {
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.Add("@Name", SqlDbType.VarChar);
        cmd.Parameters["@name"].Value = newName;
        試す
        {
            conn.Open();
            newProdID = (Int32)cmd.ExecuteScalar();
        }
        キャッチ(例外例)
        {
            Console.WriteLine(ex.Message);
        }
    }
    (int)newProdID を返します。
}

この例では、新しく追加された製品 ID を返しています。

ExecuteScalarint.parseメソッドまたはあなたが知っている最高のもので知っているように、nullをチェックして数値にキャストできるオブジェクトを返します。

于 2013-11-11T07:19:42.360 に答える