Tags:

String StrCon = ("Data Source=COIL_SVR1;Initial Catalog=TCPJtest2;User ID=sa;Password=coilsql");
String StrOut = ("SELECT col1, col2, col3, col4, col5 FROM checkTable");

SqlConnection Conn = new SqlConnection(StrCon);
SqlCommand Comm1 = new SqlCommand(StrOut, Conn);
Comm1.CommandType = CommandType.Text;

Conn.Open();
SqlDataReader DR1 = Comm1.ExecuteReader();
if (DR1.Read())
{
string col1txt = DR1.GetValue(0).ToString();
string col2txt = DR1.GetValue(1).ToString();
string col3txt = DR1.GetValue(2).ToString();
string col4txt = DR1.GetValue(3).ToString();
string col5txt = DR1.GetValue(4).ToString();

         string y = "2"; // ค่าสมมติ

    if (y == col2txt)
            {
        Label1.Text = "check if";
            }
            else
                    Label1.Text = "check else";
}

จาก code นะครับ ผมสมมติให้ y เป็นค่าที่รับเข้ามา เมื่อนำมาเช็คกับ col1txt(column1 ใน DB) ถ้าในตาราง sql DB ของผม col1txt = 1 ผลลัพธ์ที่ได้คือ check if
แต่ในกรณีของ col2txt (ก็คือ column2) ไม่ว่าข้อมูลจะเป็นข้อมูลใด เมื่อนำไปเช็คกับ y ที่กำหนดให้มีค่าเท่ากัน ผลลัพธ์ที่ได้จึงเป็น check else ตลอดเลยครับ ตัวอย่าง
col2txt ใน db = 5 --->กำหนด string y = "5"; // ค่าสมมติ

    if (y == col2txt)
            {
        Label1.Text = "check if";
            }
            else
                    Label1.Text = "check else";

result =>> check else
คือผมต้องแก้ยังไงครับ มือใหม่ อาจจะถามแบบ งงๆ รบกวนด้วยครับ ข้อมูลต้องเป็น string นะครับ

Get latest news from Blognone
By: nstk
ContributoriPhoneWindows PhoneAndroid
on 6 June 2014 - 11:51 #710742

บอกตรงๆ งง อ่านไม่รู้เรื่อง

By: mix5003
AndroidUbuntuWindows
on 6 June 2014 - 13:39 #710779

ผมไม่แน่ใจว่า C# เหมือน Java ไหม ถ้าเหมือน
ลองแก้ตรงเงื่อนไขเป็น
if(y.Equals(col2txt))

ถ้าไม่ได้ลองดูดีๆว่าทั้ง 2 อันเหมือนกันหมดไหม มีช่องว่างหน้าหลังรึเปล่าครับ

By: kswisit
ContributoriPhoneAndroidIn Love
on 6 June 2014 - 14:44 #710803

ใน table checkTable มีกี่ record ครับ

ถ้ามีหลาย record จะรู้ได้ไงว่ากำลังอ่าน record ไหนมาเปรียบอยู่?

บางทีผลลัพธ์ที่เปรียบได้อาจเป็นการเปรียบเทียบ record สุดท้ายที่ col2 != y ก็ได้

ลอง response.write(col2txt) ออกมาดูไหม?

ว่าแต่ C# ใช่ DR1.GetValue(0) เหรอ? ไม่ใช่ DR1.GetValue[0] เหรอครับ

และอันที่จริงต้องเป็น

DR1.GetValue[record][column]

DR1.GetValue[0][1] // record แรก column ที่ 2

รึเปล่า? ผมไม่ชัวร์นะ


^
^
that's just my two cents.

By: Architec
ContributorWindows PhoneAndroidWindows
on 7 June 2014 - 02:46 #710986

เป็นผมจับยัดลง DataSet, datatable เวลาไล่อ่านมันจะสะดวก และก็แก้โค้ดทีหลังง่าย แต่รกสุดๆ

แนะนำว่าให้หัดใช้ Breakpoint ครับ จะได้รู้ว่าแต่ละ Statement ทำงานถูกต้องหรือเปล่า