Skip to content

Instantly share code, notes, and snippets.

@auycro
Last active August 22, 2016 07:37
Show Gist options
  • Save auycro/0e5ce867a1962b356c9c646796d7722a to your computer and use it in GitHub Desktop.
Save auycro/0e5ce867a1962b356c9c646796d7722a to your computer and use it in GitHub Desktop.
sample query by c# (no testing)
// Created by Auycro on August 22,16.
// Copyright © 2016 Auycro. All rights reserved.
public class SampleConnectAndRunQuery
{
static string insert_sql = "INSERT INTO {0} VALUE ({1},\"{2}\")";
static bool InsertData(string table, int id, string update_detail){
bool result = true;
try {
System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection("YOUR CONNECTION STRING");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = String.Format(insert_sql,table,id.toString(),update_detail);
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
result = true;
} catch (Exception e){
Console.WriteLine(String.Format("ERROR : "+e.message));
} finally {
return result;
}
}
static void Main() {
Console.WriteLine("Program Begin");
string _table = "Foo";
int _id = 14;
string details = "Bar";
if (InsertData(_table,_id,_details)){
Console.WriteLine("Data insert completed"));
} else {
Console.WriteLine("Data insert incompleted"));
}
Console.WriteLine("Program End");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment