code_junkie

C# How to connect to MS Access 2007

Nov 14th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. private void btnSave_Click(object sender, EventArgs e)
  2. {
  3. OleDbConnection Conn = new OleDbConnection();
  4.  
  5. try
  6. {
  7. string conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ Directory.GetCurrentDirectory() +"\dvd_manager.accdb;Persist Security Info=False;";
  8. Conn.ConnectionString = conn;
  9.  
  10. Conn.Open();
  11.  
  12. int i = cbbLocatie.SelectedIndex + 65;
  13. char c = (char)i;
  14.  
  15. string sql = "INSERT INTO DVD (titel, locatie)VALUES(@titel, @locatie)";
  16. OleDbCommand Com = new OleDbCommand();
  17. Com.CommandText = sql;
  18. Com.Connection = Conn;
  19.  
  20. OleDbParameter Param = new OleDbParameter("@titel", txtTitle.Text);
  21. Com.Parameters.Add(Param);
  22.  
  23. Param = new OleDbParameter("@locatie", c);
  24. Com.Parameters.Add(Param);
  25.  
  26. Com.ExecuteNonQuery();
  27. Conn.Close();
  28.  
  29. MessageBox.Show("Data is opgeslagen " + sql);
  30. }
  31. catch (Exception ex)
  32. {
  33. MessageBox.Show("Fout opgetreden: " + ex.Message);
  34. }
  35. finally
  36. {
  37. Conn.Close();
  38. }
  39. }
  40.  
  41. try
  42. {
  43. string conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ Directory.GetCurrentDirectory() +"\dvd_manager.accdb;Persist Security Info=False;";
  44. Conn.ConnectionString = conn;
  45.  
  46. // Create object
  47. Medium M = new Medium();
  48. int i = cbbLocatie.SelectedIndex + 65;
  49. char c = (char)i;
  50.  
  51. M.Location = c;
  52. M.Title = txtTitle.Text;
  53.  
  54. Conn.Open();
  55.  
  56. string sql = "INSERT INTO DVD (titel, locatie)VALUES(@titel, @locatie)";
  57. OleDbCommand Com = new OleDbCommand();
  58. Com.CommandText = sql;
  59. Com.Connection = Conn;
  60.  
  61. OleDbParameter Param1 = new OleDbParameter("@titel", M.Title);
  62. Com.Parameters.Add(Param1);
  63.  
  64. OleDbParameter Param2 = new OleDbParameter("@locatie", M.Location);
  65. Com.Parameters.Add(Param2);
  66.  
  67. int ret = Com.ExecuteNonQuery();
  68. Conn.Close();
  69.  
  70. MessageBox.Show("Data is opgeslagen " + ret);
  71. }
  72. catch (OleDbException ex)
  73. {
  74. MessageBox.Show(ex.Message);
  75. }
  76. catch (Exception ex)
  77. {
  78. MessageBox.Show("Fout opgetreden: " + ex.Message);
  79. }
  80. finally
  81. {
  82. Conn.Close();
  83. }
  84.  
  85. // some code
  86. Conn.Open();
  87.  
  88. string sql = "INSERT INTO DVD (titel, locatie)VALUES(?, ?)";
  89. OleDbCommand Com = new OleDbCommand();
  90. Com.CommandText = sql;
  91. Com.Connection = Conn;
  92.  
  93. OleDbParameter Param1 = new OleDbParameter("@p1", OleDbType.VarChar, 1);
  94. Param1.Value = M.Title;
  95. Com.Parameters.Add(Param1);
  96.  
  97. OleDbParameter Param2 = new OleDbParameter("@p2", OleDbType.VarChar, 255);
  98. Param2.Value = M.Location;
  99. Com.Parameters.Add(Param2);
  100.  
  101. int ret = Com.ExecuteNonQuery();
  102. Conn.Close();
  103. // morde code
  104.  
  105. string sql = "INSERT INTO DVD (titel, locatie)VALUES(?, ?)";
  106.  
  107. using(OleDbConnection connection = new OleDbConnection(CONNECTION_STRING))
  108. {
  109. using(OleDbCommand command = connection.CreateCommand())
  110. {
  111. command.CommandType = CommandType.Text;
  112. command.CommandText = "INSERT INTO DVD(title,locatie)VALUES(?,?)";
  113. command.Parameters.Add("@p1", OleDbType.VarChar, 1).Value = M.Title;
  114. command.Parameters.Add("@p2", OleDbType.VarChar, 255).Value = M.Location;
  115.  
  116. connection.Open();
  117. int ret = command.ExecuteNonQuery();
  118. }
  119. }
  120.  
  121. {
  122. OleDbCommand myCommand;
  123. DbParameter parameter = myCommand.CreateParameter();
  124. parameter.ParameterName = paramAT;
  125. parameter.Value = paramTxt;
  126. myCommand.Parameters.Add(parameter);
  127.  
  128. }
  129.  
  130. {
  131. try
  132.  
  133. {
  134. string strSqldvd ="INSERT INTO DVD(title,locatie)VALUES(@title,@locate?)";
  135.  
  136.  
  137. myCommand = (OleDbCommand)dbconn.MyProvider.CreateCommand();
  138. dbconn.MyConnection.Open();
  139. myCommand.Connection = dbconn.MyConnection;
  140. myCommand.CommandText = strSqldvd;
  141. setParameter("@title",M.Title );
  142. setParameter("@locate", M.Location);
  143.  
  144. }
  145. catch (Exception)
  146. {
  147.  
  148. throw new ArgumentException();
  149. }
  150.  
  151. int count = myCommand.ExecuteNonQuery();
  152. dbconn.MyConnection.Close();
  153. return count;
  154. }
Add Comment
Please, Sign In to add comment