Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Erreur lors de l'insertion d'une valeur contenant une apostrophe

Essayez celui-ci -

IF OBJECT_ID(N'dbo.sample') IS NOT NULL
   DROP TABLE dbo.[sample]

CREATE TABLE dbo.[sample]
(
      id INT IDENTITY(1,1) PRIMARY KEY
    , name VARCHAR(20)
    , Yell_Your_self VARCHAR(2000)
)

INSERT INTO dbo.[sample] 
VALUES
    ('ganesh', 'welcome to india'), 
    ('ganesh', 'welcome to india''s largest temple')

Mise à jour 3 :

public int get(string val1,string val2) 
{ 
    using(SqlConnection con = new SqlConnection(connectionString))
    {
        con.Open();

        int i = 0;

        using (SqlCommand cmd = new SqlCommand("INSERT INTO dbo.sample (name, Yell_Your_self) VALUES(@val1, @val2)", con))
        {
            cmd.Parameters.AddWithValue("@val1", val1);
            cmd.Parameters.AddWithValue("@val2", val2);
            i = cmd.ExecuteNonQuery();
        }

        con.Close();
        return i;
    }
}