Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Visual J# / March 2005

Tip: Looking for answers? Try searching our database.

Database Help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Cpierswim - 09 Mar 2005 04:21 GMT
I've never done a database before, and I need some help.

How do I setup a Microsoft Access database (I only need a very lightwight
one, I'm not doing anything fancy with it) and connect to it in my J# program?
Lars-Inge T?nnessen [VJ# MVP] - 09 Mar 2005 20:51 GMT
Hello!

I don't know how much you know about databases, and how much I should
explain, but here is a simple example with MS Access and J#.

A good staring point could be a "ADO.NET" book, if you want in depth
knowledge.

1. Open ACCESS
2. File + New + "Blank database"
3. Name : "testDB" + "Create"
4. "Create table in Design View"
5. Filed Name -> "numb". DataType -> "Number"
6. Field name -> "val", DataType -> "Text"
7. File + Save -> Table Name "testTable"
8. "Yes" for a primary key

9. Open Visual Studio
10. New a new J# project (Windows Forms application):

In the "Server explorer", "Add connection" (right click) + "Provider" tab
11. Choose a "Microsoft JET 4.0 OLE DB Provider" + "Next"
12. Find the "testDB" file in the "Select or enter a database name".
13, Click the "test connection" button to see if the connection was ok. +
"OK"

14. Drag "ACCESS,c:\testDB.mdb.Admin" onto the design view in Visual Studio
15. choose "include password".

16. Write the SQL code. See below for example:

package Access_J;

import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;

/**
* Summary description for Form1.
*/
public class Form1 extends System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ListView listView1;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
/**
 * Required designer variable.
 */
private System.ComponentModel.Container components = null;

public Form1()
{
 //
 // Required for Windows Form Designer support
 //
 InitializeComponent();

 //
 // TODO: Add any constructor code after InitializeComponent call
 //
}

/**
 * Clean up any resources being used.
 */
protected void Dispose(boolean disposing)
{
 if (disposing)
 {
  if (components != null)
  {
   components.Dispose();
  }
 }
 super.Dispose(disposing);
}

#region Windows Form Designer generated code
/**
 * Required method for Designer support - do not modify
 * the contents of this method with the code editor.
 */
private void InitializeComponent()
{
 this.textBox1 = new System.Windows.Forms.TextBox();
 this.button1 = new System.Windows.Forms.Button();
 this.label1 = new System.Windows.Forms.Label();
 this.button2 = new System.Windows.Forms.Button();
 this.listView1 = new System.Windows.Forms.ListView();
 this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
 this.SuspendLayout();
 //
 // textBox1
 //
 this.textBox1.set_Location(new System.Drawing.Point(24, 32));
 this.textBox1.set_Name("textBox1");
 this.textBox1.set_Size(new System.Drawing.Size(248, 20));
 this.textBox1.set_TabIndex(0);
 this.textBox1.set_Text("textBox1");
 //
 // button1
 //
 this.button1.set_Location(new System.Drawing.Point(24, 64));
 this.button1.set_Name("button1");
 this.button1.set_TabIndex(1);
 this.button1.set_Text("insert data");
 this.button1.add_Click( new System.EventHandler(this.button1_Click) );
 //
 // label1
 //
 this.label1.set_Location(new System.Drawing.Point(24, 0));
 this.label1.set_Name("label1");
 this.label1.set_TabIndex(2);
 this.label1.set_Text("Insert Value");
 //
 // button2
 //
 this.button2.set_Location(new System.Drawing.Point(24, 152));
 this.button2.set_Name("button2");
 this.button2.set_TabIndex(3);
 this.button2.set_Text("get data");
 this.button2.add_Click( new System.EventHandler(this.button2_Click) );
 //
 // listView1
 //
 this.listView1.set_Location(new System.Drawing.Point(24, 184));
 this.listView1.set_Name("listView1");
 this.listView1.set_Size(new System.Drawing.Size(248, 97));
 this.listView1.set_TabIndex(4);
 //
 // oleDbConnection1
 //
 this.oleDbConnection1.set_ConnectionString("Jet OLEDB:Global Partial Bulk
Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database L" +
  "ocking Mode=1;Jet OLEDB:Database Password=;Data
Source=\"F:\\testDB.mdb\";Password=" +
  ";Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk
Transactions=1;Provider=\"Microsof" +
  "t.Jet.OLEDB.4.0\";Jet OLEDB:System database=;Jet
OLEDB:SFP=False;Extended Propert" +
  "ies=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet
OLEDB:Create Syst" +
  "em Database=False;Jet OLEDB:Don\'t Copy Locale on Compact=False;Jet
OLEDB:Compact" +
  " Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt
Database=False");
 //
 // Form1
 //
 this.set_AutoScaleBaseSize(new System.Drawing.Size(5, 13));
 this.set_ClientSize(new System.Drawing.Size(292, 294));
 this.get_Controls().Add(this.listView1);
 this.get_Controls().Add(this.button2);
 this.get_Controls().Add(this.label1);
 this.get_Controls().Add(this.button1);
 this.get_Controls().Add(this.textBox1);
 this.set_Name("Form1");
 this.set_Text("Form1");
 this.ResumeLayout(false);

}
#endregion

/**
 * The main entry point for the application.
 */
/** @attribute System.STAThread() */
public static void main(String[] args)
{
 Application.Run(new Form1());
}

// Insert data
private void button1_Click (Object sender, System.EventArgs e)
{
 int number = 1;
 String sql = "INSERT INTO testTable (numb, val) VALUES ("+number+",
'"+this.textBox1.get_Text()+"')";

 System.Data.OleDb.OleDbCommand command =
  new System.Data.OleDb.OleDbCommand( sql, this.oleDbConnection1 );

 this.oleDbConnection1.Open();
 command.ExecuteScalar();
 this.oleDbConnection1.Close();
}

// Gets all data from the DB
private void button2_Click (Object sender, System.EventArgs e)
{
 String sql = "SELECT * FROM testTable";
 System.Data.OleDb.OleDbDataAdapter dataAdaper = new
System.Data.OleDb.OleDbDataAdapter( sql, this.oleDbConnection1 );

 System.Data.DataTable _dt = new System.Data.DataTable();
 dataAdaper.Fill( _dt );

 for( int rowCounter = 0; rowCounter < _dt.get_Rows().get_Count();
rowCounter++)
 {
  System.Data.DataRow _dr = _dt.get_Rows().get_Item(rowCounter);

  System.Windows.Forms.ListViewItem _lvi = new
System.Windows.Forms.ListViewItem();
  _lvi.set_Text( _dr.get_Item("val").toString() );
  this.listView1.get_Items().Add( _lvi );
 }

}
}

Regards,
Lars-Inge T?nnessen

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.