.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

อยากทราบโค๊ดการตรวจสอบ username และ password ไม่ให้ซ้ำกันใน database Options · View
่ำjeg
Posted: Saturday, October 25, 2008 3:06:29 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 10/25/2008
Posts: 4

อยากทราบโค๊ดที่ใช้ตรวจสอบข้อมูล ก่อน insert ลงในฐานข้อมูลโดยไ่ม่ให้ username และ  password ไม่ให้ซ้ำกัน ค่ะ ถ้าซ้ำ ก็ให้ขึ้น คำว่ามีคนใช้แล้ว แต่ถ้าไม่ซ้ำ ก็ให้ add ลงฐานได้เลยค่ะ ใช้ฐานข้อมูล sql server 2005 ตอนนี้ทำยังไงก็ยังทำไม่ได้

mrxc
Posted: Saturday, October 25, 2008 3:11:24 PM
Rank: มือเทพ
Groups: Member

Joined: 3/6/2008
Posts: 196
Location: TH

1. Query UserName ที่จะแอดออกมา

 1.1 ถ้ามีบอกว่ามีแล้ว

 1.2 ถ้าไม่มีทำต่อ

2. แอดเข้าไป จบ

่ำjeg
Posted: Saturday, October 25, 2008 3:31:58 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 10/25/2008
Posts: 4

^

^

ขอบคุณสำหรับคำแนะนำค่ะ

แต่ว่าลองทำดูแล้วแต่ว่าทำไม่ได้ เพิ่งเริ่มหัดเขียน c#.net

ใครก็ได้ช่วยแนะนำเพิ่มเติมได้ป่ะคะ Pls..

mrxc
Posted: Saturday, October 25, 2008 6:04:35 PM
Rank: มือเทพ
Groups: Member

Joined: 3/6/2008
Posts: 196
Location: TH
DataTsble dx = Query("select username from user where username='" + username + "'");

if (dx.Rows.Count > 0)

{

  //("Error");

}

else

{

  // Insert ได้

}

โดยที่ Query เป็น Method ที่โหลดข้อมูลออกมาในรูป DataTable ซึ่งตรงนั้นคุณจะไปสร้างไว้ยังไงก็ได้

่ำjeg
Posted: Sunday, October 26, 2008 2:38:17 AM
Rank: มือฝึกหัด
Groups: Member

Joined: 10/25/2008
Posts: 4

        SqlConnection con = new SqlConnection(@"Server=.\SqlExpress;Database=RouteData;Integrated Security=SSPI");
    
        SqlCommand com = new SqlCommand("select username from admin where username=@username",con);
        con.Open();

        SqlDataReader rd = com.ExecuteReader();
        DataTable dt = new DataTable();

        if (dt.Rows.Count>0)
        {
            Response.Write("มี username แล้ว");
        }
        else
        {
            Response.Write("ไม่มี username นี้");
        }

 

------------------------------------------------------------------------------

เกิด error ขึ้นดังรูปค่ะค่ะ,,ไม่รู้ว่าควรจะแก้ตรงไหนดี ??

paedotnet
Posted: Sunday, October 26, 2008 10:07:19 AM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 354
Location: bkk
ถ้าจะใช้ parameter ก็ให้สร้าง Parameter ด้วยนะครับดังนี้
SqlCommand com = new SqlCommand("select username from admin where username=@username", con);
            SqlParameter usernameParam = new SqlParameter();
            usernameParam.ParameterName = "@username";
            usernameParam.SqlDbType = SqlDbType.NVarChar;
            usernameParam.Value = "ค่าตรวจสอบ username";
            com.Parameters.Add(usernameParam);
            con.Open();

            SqlDataReader rd = com.ExecuteReader();


[With great power comes great responsibility]
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by