Hi,
I need to export a table data in Access Database into a text file using C# interface by a press of a button. I need advise on which method is required to use for this Tks.
Hi,
I need to export a table data in Access Database into a text file using C# interface by a press of a button. I need advise on which method is required to use for this Tks.
Export table data in Access Database
BobConsultant
You should indicate the namespace, for example, to use the StreamWriter and server class, you should add
using System.IO;
using Microsoft.SqlServer.Management.Smo;
stupots
The declaration and error as follow:
using
System.IO;using
Microsoft.SqlServer.Management.Smo;Error:
The type or namespace name 'SqlServer' does not exist in the class or namespace 'Microsoft' (are you missing an assembly reference )
Jithendrian
Barry Kwok
Hope following codes are helpful for you. But I haven't compiled and test them, just for your reference.KerryLW
I tested on my Login Table but received the following error:
private void btnAR_Click(object sender, System.EventArgs e)
{
//Pass the filepath and filename to the StreamWriter Constructor
StreamWriter sw = new StreamWriter("C:\\JSM.txt");
string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("C:/Database.mdb");
string strSQL = "SELECT * FROM Login where id=" + Convert.ToInt32(Request.QueryString["User_ID"]) ;
// create Objects of ADOConnection and ADOCommand
OleDbConnection myConn = new OleDbConnection(strDSN);
OleDbCommand myCmd = new OleDbCommand( strSQL, myConn );
OleDbDataReader datareader = null;
try
{
myConn.Open();
datareader = myCmd.ExecuteReader();
while (datareader.Read() )
{
//Write a line of text
sw.WriteLine("datareader["User_ID"].ToString()+" "+datareader["Password"].ToString());
}
}
catch (Exception e)
{
string Messate = e.Message;
}
finally
{
myConn.Close();
}
}
) expected
; expected
; expected
; expected
; expected
; expected
Newline in constant
danleis
Sorry, it should be
sw.WriteLine(datareader["User_ID"].ToString()+" "+datareader["Password"].ToString());
}
No the first quotation mark before the "datareader"
idos
Ron DeSerranno - MOBIFORM
I have tried and compile without error, but it does not seems to be running. I put an Invoke message to test but when I click the button, nothing happens.
-------------------
private void btnAR_Click(object sender, System.EventArgs e)
{
//Pass the filepath and filename to the StreamWriter Constructor
StreamWriter sw = new StreamWriter("C:/JSM.txt");
string database = "C:/M8099_Project/Database/Invoicing_Database.mdb";
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + database;
string strSQL = "SELECT * FROM Login WHERE User_ID = 1";
// create Objects of ADOConnection and ADOCommand
OleDbConnection myConn = new OleDbConnection(strConn);
OleDbCommand myCmd = new OleDbCommand(strSQL, myConn);
OleDbDataReader datareader = null;
try
{
myConn.Open();
datareader = myCmd.ExecuteReader();
while (datareader.Read() )
{
//Write a line of text
sw.WriteLine(datareader["User_ID"].ToString()+" "+datareader["Password"].ToString());
string message = "Export complete!";
string caption = "Export Database!";
MessageBoxButtons buttons = MessageBoxButtons.OK;
MessageBoxIcon icon = MessageBoxIcon.Warning;
DialogResult result;
// Displays the MessageBox
result = MessageBox.Show(this, message, caption, buttons, icon);
}
}
catch //(Exception e)
{
string message = "Export error!";
string caption = "Export Warning!";
MessageBoxButtons buttons = MessageBoxButtons.OK;
MessageBoxIcon icon = MessageBoxIcon.Warning;
DialogResult result;
// Displays the MessageBox
result = MessageBox.Show(this, message, caption, buttons, icon);
}
finally
{
myConn.Close();
}
}
pdns
BobLoblaw
In winform application, connection string should be something like:
string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= D:\App_Data\Northwind.mdb");
query statement should be something like:
string strSQL = "SELECT * FROM Login where id=" + User_ID ;//User_ID should be a string
please modify the code above according to your reqirement
Quinn01
Rhubarb
Some missing directive or assembly reference error again. Is it because of the SQL
private void btnAR_Click(object sender, System.EventArgs e)< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
{
//Pass the filepath and filename to the StreamWriter Constructor
StreamWriter sw = new StreamWriter("C:\\JSM.txt");
string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("C:/M8099_Project/Database/Invoicing_Database.mdb");
string strSQL = "SELECT * FROM Login where id=" + Convert.ToInt32(Request.QueryString["User_ID"]) ;
// create Objects of ADOConnection and ADOCommand
OleDbConnection myConn = new OleDbConnection(strDSN);
OleDbCommand myCmd = new OleDbCommand(strSQL, myConn);
OleDbDataReader datareader = null;
try
{
myConn.Open();
datareader = myCmd.ExecuteReader();
while (datareader.Read() )
{
//Write a line of text
sw.WriteLine(datareader["User_ID"].ToString()+" "+datareader["Password"].ToString());
}
}
catch
{
string message = "Export error!";
string caption = "Export Warning!";
MessageBoxButtons buttons = MessageBoxButtons.OK;
MessageBoxIcon icon = MessageBoxIcon.Warning;
DialogResult result;
// Displays the MessageBox
result = MessageBox.Show(this, message, caption, buttons, icon);
}
finally
{
myConn.Close();
}
}
The type or namespace name 'StreamWriter' could not be found (are you missing a using directive or an assembly reference )
The type or namespace name 'Server' could not be found (are you missing a using directive or an assembly reference )
The type or namespace name 'Request' could not be found (are you missing a using directive or an assembly reference )
The type or namespace name 'sw' could not be found (are you missing a using directive or an assembly reference )
prthealien
Did you read my last post
Is this a windows or web application
Meir P
This is the error I get if I quote the //using Microsoft.SqlServer.Management.Smo;
I have a MS Access Database with a Table name "Login" and Column 1 "User_ID" and "Password". The User_ID and Password is the actual column name I used in the MS Access.
--------------
StreamWriter sw = new StreamWriter("C:\\JSM.txt");
string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("C:/M8099_Project/Database/Invoicing_Database.mdb");
string strSQL = "SELECT * FROM Login where id=" + Convert.ToInt32(Request.QueryString["User_ID"]) ;
// create Objects of ADOConnection and ADOCommand
OleDbConnection myConn = new OleDbConnection(strDSN);
OleDbCommand myCmd = new OleDbCommand(strSQL, myConn);
OleDbDataReader datareader = null;
try
{
myConn.Open();
datareader = myCmd.ExecuteReader();
while (datareader.Read() )
{
//Write a line of text
sw.WriteLine(datareader["User_ID"].ToString()+" "+datareader["Password"].ToString());
}
}
catch
{
string message = "Export error!";
string caption = "Export Warning!";
MessageBoxButtons buttons = MessageBoxButtons.OK;
MessageBoxIcon icon = MessageBoxIcon.Warning;
DialogResult result;
// Displays the MessageBox
result = MessageBox.Show(this, message, caption, buttons, icon);
}
finally
{
myConn.Close();
}
Error:
The type or namespace name 'Server' could not be found (are you missing a using directive or an assembly reference )
The type or namespace name 'Request' could not be found (are you missing a using directive or an assembly reference )