Inserting data into a database cannot understand

I am trying to figure out how to embed data in my database, so I looked through a lot of tutorials and I couldn't figure out how to do this. one tutorial brought me to this:

public partial class Register : System.Web.UI.Page{

public string ID, Pass, Email, BDYear, BDMonth, BDDay, FullName;
SqlCommand cmd;
SqlConnection con;
SqlDataAdapter da;
protected void Page_Load(object sender, EventArgs e)
{


    if (IsPostBack){

        ID = Request.Form["ID"];
        Pass = Request.Form["PW"];
        Email = Request.Form["EMAIL"];
        BDYear = Request.Form["BDYear"];
        BDMonth = Request.Form["BDMonth"];
        BDDay = Request.Form["BDDay"];
        FullName = Request.Form["FullName"];

        cmd = new SqlCommand("INSERT INTO UserInfo (ID, Pass, Email, BDYear, BDMonth, BDDay, FullName) VALUES (ID, Pass, Email,BDYear, BDMonth, BDDay, FullName)");

    }

}
}

But this does not really work or does not show a sign that it is working, and I think I need the help of someone who tells me what to do in my situation. I do not know if what is written here is written correctly, but please, I need guidance. All variables are set on the aspx page according to these names.

+4
source share
4 answers

You should try something like this:

  • configure the query statement as a string
  • SqlCònnection SqlCommand using(..) { ... }
  • ,
  • , , .

:

   -- your INSERT statement:
   string query = "INSERT INTO UserInfo(ID, Pass, Email, BDYear, BDMonth, BDDay, FullName) " + 
                  "VALUES (@ID, @Pass, @Email, @BDYear, @BDMonth, @BDDay, @FullName);";

   -- define your connection to the database
   using (SqlConnection conn = new SqlConnection("server=.;database=test;Integrated Securiy=SSPI;"))
   -- define your SqlCommand
   using (SqlCommand cmd = new SqlCommand(query, conn))
   {
       -- define the parameters and set their values
       cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
       cmd.Parameters.Add("@Pass", SqlDbType.VarChar, 50).Value = Pass;
       cmd.Parameters.Add("@Email", SqlDbType.VarChar, 255).Value = Email;
       cmd.Parameters.Add("@BDYear", SqlDbType.Int).Value = BDYear;
       cmd.Parameters.Add("@BDMonth", SqlDbType.Int).Value = BDMonth;
       cmd.Parameters.Add("@BDDay", SqlDbType.Int).Value = BDDay;
       cmd.Parameters.Add("@Fullname", SqlDbType.VarChar, 200).Value = Fullname;

       -- open connection, execute query, close connection
       conn.Open();

       int rowsAffected = cmd.ExecuteNonQuery();

       conn.Close();
   }

, , - , , ! : SqlConnection, , - , , - !

+5

, .

MSDN: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection%28v=vs.110%29.aspx

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}

, .

0

sqlconnection, .net , , ..

sqlconnection con;
sqlcommand cmd;

con = new sqlconection("your connection string goes here");
cmd = new sql command("your query", con); //we are telling cmd that you need to
// fire the query using con which is a connection object which ultimately
// contains database connection information
con.open();
cmd.ExecuteNonQuery();
con.close();

, . "select". .

: -

asp.net #

0
        cmd = new SqlCommand("INSERT INTO UserInfo (ID, Pass, Email, BDYear, BDMonth, BDDay, FullName) VALUES ("+ID+", "+Pass+", "+Email+","+BDYear+", "+BDMonth+", "+BDDay+", "+FullName+")");

, SqlCommand .

.

http://msdn.microsoft.com/tr-tr/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx http://www.csharp-station.com/Tutorial/AdoDotNet/ lesson06

-1
source

Source: https://habr.com/ru/post/1568512/


All Articles