Insert data into SQL Server from C# code

前端 未结 7 1857
自闭症患者
自闭症患者 2021-01-06 06:59

I have a table student (id, name). Then I have one textbox, for entering the name, when click on submit button, it inserts the data into the databa

相关标签:
7条回答
  • 2021-01-06 07:05

    You better use parameters when you insert data.

    try
    {
        string sql = "insert into student(name) values (@name)";
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                cmd.Parameters.AddWithValue("@name", test); // assign value to parameter 
                cmd.ExecuteNonQuery();
            }
        }
    }
    catch (SqlException ex)
    {
        string msg = "Insert Error:";
        msg += ex.Message;
    }
    
    0 讨论(0)
  • 2021-01-06 07:05
    string sql = "INSERT INTO student (name) values (@name)";
    conn.Open();
    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.Parameters.Add("@name", SqlDbType.VarChar);
    cmd.Parameters["@name"].Value = test;
    cmd.ExecuteNonQuery();
    
    0 讨论(0)
  • 2021-01-06 07:05

    use the key word "identity" to auto increment the id column

    Refer : http://technet.microsoft.com/en-us/library/aa933196(v=sql.80).aspx

    create table table_name( id int PRIMARY KEY IDENTITY ) 
    

    and you no need to mention the "id" in the insert query

    0 讨论(0)
  • 2021-01-06 07:06

    Try the following query,

    insert into student(name) values(name) 
    

    SQL Server internally auto increments the id column when u insert the data since u said it is auto increment. If it is not working, the u have to check the identity column in the db.

    0 讨论(0)
  • 2021-01-06 07:16

    I was facing this problem and after trying various solution found at stack overflow, i could summarize the experience as follows: commands executed in command shell of mssql like:

    insert into table_name (val1,val2,val3,val4) VALUES ("val1","val2",0,"val4")

    go

    or

    insert into table_name VALUES ("val1","val2",0,"val4")

    go
    

    work when typed directly in the mssql database prompt,

    But when it is required to use the the insert statement from c#, it is required to be kept in mind that string needs to be surrounded by an additional pair of single quites, around the strings, like in:

    SqlConnection cnn;
    
    string connetionString = "Data Source=server_name;Initial Catalog=database_name;User ID=User_ID;Password=Pass_word";
    
    cnn = new SqlConnection(connetionString);
    
    SqlCommand myCommand = new SqlCommand("insert into table_name (val1,val2,val3,val4) VALUES ('val1','val2',0,'val4');", cnn);
    
    //or
    //SqlCommand myCommand = new SqlCommand(insert into table_name VALUES ('val1','val2',0,'val4');", cnn);
    
    cnn.Open();
    
    myCommand.ExecuteNonQuery();
    
    cnn.Close();
    

    the problem here is that most people, like myself, try to use <\"> in the place of double quotes <">that is implemented as in the above command line case, and SQL executor fails to understand the meaning of this.

    Even in cases where a string needs to be replace, ensure that strings are surrounded by single quotation, where a string concatination looks like a feasible solution, like in:

    SqlCommand myCommand = new SqlCommand("insert into table_name (val1,val2,val3,val4) VALUES ('"+val1+"','val2',0,'val4');", cnn);
    
    0 讨论(0)
  • 2021-01-06 07:22

    You don't need to mention the ID in first part.

    insert into student(name) values('name') 
    
    0 讨论(0)
提交回复
热议问题