inserting data into SQL Database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • djozy

    #1

    inserting data into SQL Database

    Please,
    I want to insert data into SQL Server database. I know for
    this commmand:
    SqlCommand myCommand= new SqlCommand("INS ERT INTO table
    (Column1, Column2) " +
    "Values ('string', 1)", myConnection);
    ,but how to insert,lets say,a string from textbox1? Or
    datetime from textbox2?
    Thank you
    djozy
  • Brian M. Reisman

    #2
    Re: inserting data into SQL Database

    I'd recommend using the parameters collection rather than concatenating SQL
    Statement into a string (Which is susceptible to SQL Injection exploits),
    for example using SqlDataAdapter wizard code you would add the following
    line:

    SqlInsertComman d1.Parameters["@LastName"].Value = textBox1.Text;

    I included some IDE generated code at the bottom of the email so you can see
    how the above line relates to the SqlCommand as a whole.
    --
    Brian M. Reisman
    MCAD, MCDBA, MCSD,
    MCSE, MCT, OCA, NET+
    My Book @ Amazon: http://www.amazon.com/exec/obidos/tg...l/-/0782141617
    [color=blue][color=green]
    >>Wizard generated code:[/color][/color]

    this.sqlInsertC ommand1.Command Text = @"INSERT INTO Employees(LastN ame,
    FirstName, Title, TitleOfCourtesy , BirthDate, HireDate, Address, City,
    Region, PostalCode, Country, HomePhone, Extension, Photo, Notes, ReportsTo,
    PhotoPath) VALUES (@LastName, @FirstName, @Title, @TitleOfCourtes y,
    @BirthDate, @HireDate, @Address, @City, @Region, @PostalCode, @Country,
    @HomePhone, @Extension, @Photo, @Notes, @ReportsTo, @PhotoPath); SELECT
    EmployeeID, LastName, FirstName, Title, TitleOfCourtesy , BirthDate,
    HireDate, Address, City, Region, PostalCode, Country, HomePhone, Extension,
    Photo, Notes, ReportsTo, PhotoPath FROM Employees WHERE (EmployeeID =
    @@IDENTITY)";

    this.sqlInsertC ommand1.Connect ion = this.sqlConnect ion2;

    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@LastName ",
    System.Data.Sql DbType.NVarChar , 20, "LastName") );
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@FirstNam e",
    System.Data.Sql DbType.NVarChar , 10, "FirstName" ));
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@Title", System.Data.Sql DbType.NVarChar ,
    30, "Title"));
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@TitleOfC ourtesy",
    System.Data.Sql DbType.NVarChar , 25, "TitleOfCourtes y"));
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@BirthDat e",
    System.Data.Sql DbType.DateTime , 8, "BirthDate" ));
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@HireDate ",
    System.Data.Sql DbType.DateTime , 8, "HireDate") );
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@Address" ,
    System.Data.Sql DbType.NVarChar , 60, "Address")) ;
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@City", System.Data.Sql DbType.NVarChar ,
    15, "City"));
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@Region",
    System.Data.Sql DbType.NVarChar , 15, "Region"));
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@PostalCo de",
    System.Data.Sql DbType.NVarChar , 10, "PostalCode "));
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@Country" ,
    System.Data.Sql DbType.NVarChar , 15, "Country")) ;
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@HomePhon e",
    System.Data.Sql DbType.NVarChar , 24, "HomePhone" ));
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@Extensio n",
    System.Data.Sql DbType.NVarChar , 4, "Extension" ));
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@Photo",
    System.Data.Sql DbType.VarBinar y, 2147483647, "Photo"));
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@Notes", System.Data.Sql DbType.NVarChar ,
    1073741823, "Notes"));
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@ReportsT o", System.Data.Sql DbType.Int,
    4, "ReportsTo" ));
    this.sqlInsertC ommand1.Paramet ers.Add(new
    System.Data.Sql Client.SqlParam eter("@PhotoPat h",
    System.Data.Sql DbType.NVarChar , 255, "PhotoPath" ));





    "djozy" <anonymous@disc ussions.microso ft.com> wrote in message
    news:048601c3ba 00$a5e1f080$a40 1280a@phx.gbl.. .[color=blue]
    > Please,
    > I want to insert data into SQL Server database. I know for
    > this commmand:
    > SqlCommand myCommand= new SqlCommand("INS ERT INTO table
    > (Column1, Column2) " +
    > "Values ('string', 1)", myConnection);
    > ,but how to insert,lets say,a string from textbox1? Or
    > datetime from textbox2?
    > Thank you
    > djozy[/color]


    Comment

    Working...