Ms Access Connect C# PDF
Ms Access Connect C# PDF
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.OleDb;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { private OleDbConnection mycon; public Form1() {
19 mycon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\user\My Documents\login.mdb;Persist Security Info=False"); 20 21 22 23 24 25 26 27 private void textBox1_TextChanged(object sender, EventArgs e) } InitializeComponent(); mycon.Open();
28 29 30 31 32 33 34 35 36 37 38 39 40
private void login_Click(object sender, EventArgs e) { string x =usename.Text; DataSet ds = new DataSet(); OleDbDataAdapter adapter =
41 new OleDbDataAdapter("Select * from login where username="+usename.Text+"and password="+passname.Text, mycon); 42 43 MessageBox.Show("The username is "+usename.Text+" and the password is"+passname.Text ,"Test Message"); 44 45 46 -------------------------------------------------------------------------------------------------------------------------------------01 02 03 04 private void login_Click(object sender, EventArgs e) { OleDBDataReader dr = null; OleDBCommand cmd = null; } }
05 string cmdStr = "SELECT * FROM login WHERE username='"+username.Text+"' and password='"passname.Text+"' LIMIT 1"; 06
07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22
cmd = new OleDBCommand(cmdStr,mycon); dr = cmd.ExecuteNonReader(); cmd.Dispose(); //Check the datareader here...if dr returns 1 record...user is in DB if(dr.Read() == true) { MessageBox.Show("Login Successful"); //User is in DB... do work here } else { MessageBox.Show("Invalid Credentials, Please Re-Enter"); } dr.Dispose();
-----------------------------------------------------------------------------------------------------------------------------------I have a database name details.mdb in Microsoft Office Access database in this data base there is one table name info it contain two coloum name and age.
I have form in that form there are two text box name TextBox1,TextBox2 and a Button1.
I want that the information entered in both textbox should stored in details.mdb database.
using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.OleDb;
namespace Project2 { public partial class Form1 : Form1 { public Form1() { InitializeComponent(); }
private void Button1_Click(object sender, EventArgs e) { //what should i write here so that it store in details.mdb database. }
} } Re: How to insert data in Microsoft Office Access database using C#? Posted on: 10 Apr 2009
Dear
OleDbConnection con=new OleDbConnection(constr); OleDbCommand com=new OleDbCommand(cmdstr,con); con.Open(); com.Parameters.AddWithValue("?",textBox1.Text); com.Parameters.AddWithValue("?",int.Parse(textBox3.Text)); com.ExecuteNonQuery(); con.Close();
using System.Data.OleDb;
Thanks
From
Rajendra -------------------------------------------------------------------------------------------------------------------
Note: In order to perform operations on M.S.Access-2007 records, M.S.Office-2007 should be installed in your system.
Introduction:
'using System.Data.OleDb'
But for accesing records from M.S.Access-2007 file we use 'Ace' driver.
In this application, we will search a record by taking input from the InputBox. For this we have to add reference to Microsoft.VisualBasic.
Adding a Reference:
Goto Project Menu ->Add Reference -> select 'Microsoft.VisualBasic' from .NET tab.
'using Microsoft.VisualBasic'
In this app. we use Find() method to search a record, which requires details of primarykey column for database tables; this is provided using statement: adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
But as we don't have any primarykey column in M.S.Access table, we have to create a primary key column in datatable.
Eg:
After searching a record, we have to get the index of that record so that we can show next and previous records when we press '>>'(next) and '<<'(previous) buttons.
Eg:
using System; using System.Data; using System.Windows.Forms; using System.Data.OleDb; using Microsoft.VisualBasic;
namespace prash_access07 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { con = new OleDbConnection(@" provider=Microsoft.ace.Oledb.12.0; data source=E:\prash\stud.accdb;Persist Security Info=False");//stud.accdb->access07 filename loaddata(); showdata(); } void loaddata() { adapter = new OleDbDataAdapter("select * from student", con); ds = new DataSet();//student-> table name in stud.accdb file adapter.Fill(ds, "student"); ds.Tables[0].Constraints.Add("pk_sno", ds.Tables[0].Columns[0], true);//creating primary key for Tables[0] in dataset dataGridView1.DataSource = ds.Tables[0]; } void showdata() { textBox1.Text = ds.Tables[0].Rows[rno][0].ToString(); textBox2.Text = ds.Tables[0].Rows[rno][1].ToString(); textBox3.Text = ds.Tables[0].Rows[rno][2].ToString(); }
private void btnPrevious_Click(object sender, EventArgs e) { if (ds.Tables[0].Rows.Count > 0) { if (rno > 0) { rno--; showdata(); } else MessageBox.Show("First Record"); } else MessageBox.Show("no records"); }
if (ds.Tables[0].Rows.Count > 0) { if (rno < ds.Tables[0].Rows.Count - 1) { rno++; showdata(); } else MessageBox.Show("Last Record");
private void btnLast_Click(object sender, EventArgs e) { if (ds.Tables[0].Rows.Count > 0) { rno = ds.Tables[0].Rows.Count - 1; showdata(); } else MessageBox.Show("no records"); }
cmd = new OleDbCommand("insert into student values(" + textBox1.Text + ",' " + textBox2.Text + " ',' " + textBox3.Text + " ')", con); con.Open(); int n = cmd.ExecuteNonQuery(); con.Close();
private void btnSearch_Click(object sender, EventArgs e) { int n = Convert.ToInt32(Interaction.InputBox("Enter sno:", "Search", "20", 200, 200) ; DataRow drow = ds.Tables[0].Rows.Find(n); if (drow != null) { rno = ds.Tables[0].Rows.IndexOf(drow); textBox1.Text = drow[0].ToString(); textBox2.Text = drow[1].ToString(); textBox3.Text = drow[2].ToString(); } else
private void btnUpdate_Click(object sender, EventArgs e) { cmd = new OleDbCommand("update student set sname='" + textBox2.Text + "',course='" + textBox3.Text + "' where sno=" + textBox1.Text, con); con.Open(); int n = cmd.ExecuteNonQuery(); con.Close(); if (n > 0) { MessageBox.Show("Record Updated"); loaddata(); } else MessageBox.Show("Update failed"); }
private void btnDelete_Click(object sender, EventArgs e) { cmd = new OleDbCommand("delete from student where sno=" + textBox1.Text, con); con.Open(); int n = cmd.ExecuteNonQuery(); con.Close(); if (n > 0) { MessageBox.Show("Record Deleted");
loaddata(); }
} } --------------------------------------------------------------------------------------------------------------------