SQL Insert Query Using C#

后端 未结 8 1711
长情又很酷
长情又很酷 2020-11-27 17:24

I\'m having an issue at the moment which I am trying to fix. I just tried to access a database and insert some values with the help of C#

The things I tried (worked)

8条回答
  •  清酒与你
    2020-11-27 18:15

    public static string textDataSource = "Data Source=localhost;Initial 
    Catalog=TEST_C;User ID=sa;Password=P@ssw0rd";
    public static bool ExtSql(string sql) {
        SqlConnection cnn;
        SqlCommand cmd;
        cnn = new SqlConnection(textDataSource);
        cmd = new SqlCommand(sql, cnn);
        try {
            cnn.Open();
            cmd.ExecuteNonQuery();
            cnn.Close();
            return true;
        }
        catch (Exception) {
            return false;
        }
        finally {
            cmd.Dispose();
            cnn = null;
            cmd = null; 
        }
    }
    

提交回复
热议问题