Insert data in your DataBase

Hello, after you have created your database and connected in it, you can input some data in the middle of the programming.

Like this example:

1: public class Program
2: {
3: static Person GetPerson()
4: {
5: Person person = new Person();
6: Console.WriteLine(“Name”);
7: person.Name = Console.ReadLine();
8: Console.WriteLine(“Email “);
9: person.Email = Console.ReadLine();
10: Console.WriteLine(“Gender (M ou F) “);
11: person.Gender= Convert.ToChar(Console.ReadLine());
12: Console.WriteLine(“Date of Born”);
13: person.DateofBorn= Convert.ToDateTime(Console.ReadLine());
14: return person;
15: }
16:
17: static void Main(string[] args)
18: {
19: //
20: //calls a method that will fill out a object Person with users input
21: //
22: Person newPerson = GetPerson();
23:
24: //
25: //string of connection that reports datas of DataBase that I will connect
26: //
27: string connectionString = @”Data Source=.\SQL;Initial Catalog=EstudyBlog;Integrated Security=True;Pooling=False”;
28:
29: //
30: // Query TSQL with command that I will realize in DataBase
31: //
32: string query = “INSERT INTO Person (name, dateBorn, gender, email) values (@name, @dateBorn, @gender, @email)”;
33:
34: SqlConnection conn = null;
35: try
36: {
37: //
38: //instance of connection
39: //
40: conn = new SqlConnection(connectionString);
41:
42: //
43: //look for if the connection is close, if it is so it will open.
44: //
45: if (conn.State == ConnectionState.Closed)
46: {
47: //
48: //open connection
49: //
50: conn.Open();
51: }
52:
53: //
54: // Creat of object command , that receives a query that will used in operation and the connection with tha DataBase.
55: //
56: SqlCommand cmd = new SqlCommand(query, conn);
57:
58: //
59: // Add parameters in command
60: //
61: cmd.Parameters.Add(new SqlParameter(“name”, newPerson.Name));
62: cmd.Parameters.Add(new SqlParameter(“dateBorn”, newPerson.DateBorn));
63: cmd.Parameters.Add(new SqlParameter(“gender”, newPerson.Gender));
64: cmd.Parameters.Add(new SqlParameter(“email”, newPerson.Email));
65:
66: //
67: // Execute command
68: //
69: cmd.ExecuteNonQuery();
70:
71: //
72: // Close the connection with DataBase
73: //
74: conn.Close();
75:
76: Console.WriteLine(“Person registered with success!!!”);
77: }
78: catch (Exception ex)
79: {
80: Console.WriteLine(ex.Message);
81: }
82: finally
83: {
84: //
85: // Ensures that the connection will be closed also that ocorres some error.
86: // Don’t exist problem in close a connection two times.
87: // The problem is in open a connection two times.
88: //
89: if (conn != null)
90: {
91: conn.Close();
92: }
93: }
94: }
95: }

Good studys for us !

Font: http://fpimentel88.wordpress.com/2009/01/18/aprendendo-c-parte-1-acessando-um-banco-sql-server-2005-com-adonet/

One thought on “Insert data in your DataBase

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.