Sql insert in Csharp

August 13, 2008 at 12:51 pm Leave a comment

public int insertdata(string forname, string lastname, string email, DateTime birthdate, string country, string username, string pword)
        {

            String select = "select * from Player where username='" + username+"'";
            SqlCommand cmd = new SqlCommand(select, conn);
            conn.Open();
            SqlDataReader rows = cmd.ExecuteReader();

            if (rows.HasRows)
            {
                return -1;
            }
            conn.Close();
            String insert = "insert into player (forname,lastname,country,email,birthdate,username,password,lastlogin) values (@forname,@lastname,@country,@email,@birthdate,@username,@password,@lastlogin)";

            cmd = new SqlCommand(insert, conn);
            cmd.Parameters.Add("@forname", System.Data.SqlDbType.VarChar).Value = forname;
            cmd.Parameters.Add("@lastname", SqlDbType.VarChar).Value = lastname;
            cmd.Parameters.Add("@country", SqlDbType.VarChar).Value = country;
            cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = email;
            cmd.Parameters.Add("@birthdate", SqlDbType.DateTime).Value = birthdate;
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
            cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = pword;
            cmd.Parameters.Add("@lastlogin", SqlDbType.DateTime).Value = DateTime.Now;

            conn.Open();
            int numRows = cmd.ExecuteNonQuery();
            conn.Close();
            if (numRows == 1)
            {

                return 0;
            }
            else
            {

                return -2;
            }

        }
Advertisements

Entry filed under: c-sharp. Tags: , , , , , , , , , , , , , , , .

Php script for email form A smart join with union

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


August 2008
M T W T F S S
« Jul   Sep »
 123
45678910
11121314151617
18192021222324
25262728293031

%d bloggers like this: