Skip to content

Instantly share code, notes, and snippets.

@rurtubia
Created April 24, 2015 17:42
Show Gist options
  • Save rurtubia/4e9266d82cded92b45a5 to your computer and use it in GitHub Desktop.
Save rurtubia/4e9266d82cded92b45a5 to your computer and use it in GitHub Desktop.
Captures text from textboxes in a Windows Form. Creates a conection to an SQL Server database. Inserts the captured information into the database.
//Creates connection
//@"Server=[local computer]"
//Integrated Security=yes, means Windows Authentication
//Database=[database name]"
SqlConnection miConexion = new SqlConnection(
@"Server=AE-PC\SQLEXPRESS;
Integrated Security=yes;
Database=clientes");
//Insertion string
//use "+ this.CSharpElement +" to add values from form.
//to capture strings use: '"+this.string+"'
//to capture numbers use: "+this.number+"
string strComando = "INSERT INTO cliente VALUES ('" +this.textBoxName.Text +"','"
+this.textBoxSurname.Text +"', "
+this.textBoxAge.Text +" );";
//Console.WriteLine(strComando);
SqlCommand miComando = new SqlCommand(strComando, miConexion);
try
{
//Opens the connection
miConexion.Open();
//Used to get the error
miComando.ExecuteNonQuery();
Console.WriteLine("Data Added");
//this.dataGridView1.Update();
}
catch (Exception Error)
{
Console.WriteLine(Error.Message);
}
finally
{
if (miConexion.State == ConnectionState.Open)
{
miConexion.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment