-1

配列を介してデータベースの値を更新していますが、配列の最後の値のみをすべての行に更新しています。私は何が間違っているのですか?

.csファイル内

BL_HotelDetails hd1 = new BL_HotelDetails();
string[] strResult = strObj.Split(',');
hd1.updateintoRoomNames(hid, strResult);

Bussinessロジックレイヤー内

public void updateintoRoomNames(int hid, string[] strResult)
{
    DA_HotelDetails hd2 = new DA_HotelDetails();
    hd2.updateintoRommNamesDA(hid,strResult);
}

データアクセス層内

public void updateintoRommNamesDA(int hid, string[] strResult)
{
    foreach (string s in strResult) 
    {
        Connection concls = new Connection();
        SqlCommand cmd = new SqlCommand();
        string instr = "update tblRoomNames set roomnames='" + s + "' where hid=" + hid + "";

        concls.opencon();
        cmd.CommandText = instr;
        concls.executenonquery(cmd);
        concls.closecon();
    }
}
4

2 に答える 2

1

あなたのコードは問題ないようです。ここでそれがどのように機能しているか

strResult に 'Value1'、'Value2'、Value3' および hid=5 の 3 つの値があるとします。

最初の繰り返しの後、部屋名は 'Value1' になり、hid = 5 になります。

2 回目の反復の後 roomnames ='Value2' ここで hid =5 3 回目の反復の後 roomnames='Value3' ここで hid =5

その結果、roomnames='Value3' が表示されます。

strResult 配列の各値の部屋名を変更する場合は、strResult に対応する hid を保持する別の整数配列を使用する必要があります。

アップデート

String strResult の配列を作成したように、hid 値を格納するための整数の配列を作成することをお勧めします。

    int[] intNameIDs = = new int[] { 3, 4, 5 };
    int hid=22;

    public void updateintoRoomNames(int hid,int[] intNameIDs, string[] strResult)
    {
        DA_HotelDetails hd2 = new DA_HotelDetails();
        hd2.updateintoRommNamesDA(hid,intNameIDs, strResult);
    }


    public void updateintoRommNamesDA(int hid,int[] intNameIDs, string[] strResult)
    {
        int nameidIndex = 0;//index for intHids array. 
        foreach (string s in strResult)
        {
            Connection concls = new Connection();
            SqlCommand cmd = new SqlCommand();
            string instr = "update tblRoomNames set roomnames='" + s + "' where nameid=" + intNameIDs[nameidIndex] + " and Hid=" + hid;

            concls.opencon();
            cmd.CommandText = instr;
            concls.executenonquery(cmd);
            concls.closecon();

            nameidIndex++;//move to next.
        }
    }
于 2013-01-28T07:45:34.950 に答える
0

私たちの議論に基づいています。このように更新できます。

    public void updateintoRommNamesDA(List<Entity_HotelDetails> updatedList)
    {

        foreach (Entity_HotelDetails s in updatedList)
        {
            Connection concls = new Connection();
            SqlCommand cmd = new SqlCommand();
            string instr = "update tblRoomNames set roomnames='" + s.ROOMNAMES + "' where nameid=" + s.NAMEID + " and Hid=" + s.HID;

            concls.opencon();
            cmd.CommandText = instr;
            concls.executenonquery(cmd);
            concls.closecon();


        }
    }
于 2013-01-28T10:33:18.390 に答える