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();
}
}