How to select All the Tables in Access DataBase 2007 using query - c#-4.0

I am working in C#.net.I want to select all table names in the Access Database using Query. Can any one tell... How to select all the tables name from access database using query?.

Try like below it will help you...
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\myAccess2007file.accdb;Persist Security Info=False;";
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
DataTable tables = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"TABLE"});
conn.Close();
Now the Datatable "tables" have all the access table Info..

Related

Node and SQL Injection by using ${variable} on query string

I was told on a question that Im having a SQL Injection problem.
Here is the question
Node with SQL Server - response with for json path query not responding as expected
and here is my code
let sqlString = `
SELECT codeid, code, validFrom, validTo,
(SELECT dbo.PLprospectAgentCodesComp.productIdentifier, dbo.masterGroupsProducts.productName, dbo.PLprospectAgentCodesComp.compensation
FROM dbo.PLprospectAgentCodesComp INNER JOIN
dbo.masterGroupsProducts ON dbo.PLprospectAgentCodesComp.productIdentifier = dbo.masterGroupsProducts.productIdentifier
WHERE (dbo.PLprospectAgentCodesComp.codeid = dbo.PLprospectAgentCodes.codeid) for json path ) as products
FROM dbo.PLprospectAgentCodes
WHERE (plid = ${userData.plid}) for json path`
let conn = await sql.connect(process.env.DB_CONNSTRING)
let recordset = await conn.query(sqlString)
But I've read at Microsoft, and even on a question on this site, that that format prevents SQL injection.
From MS:
"All values are automatically sanitized against sql injection. This is
because it is rendered as prepared statement, and thus all limitations
imposed in MS SQL on parameters apply. e.g. Column names cannot be
passed/set in statements using variables."
I was trying to use the declare #parameter for the above code, but since my code has several queries that depend one of another, Im using await for each Query... and #parameter is not working. After I process the recordset, other queries will execute.
If my code actually is dangerous for SQL injection, is it possible to sanitize sqlString before the following two lines? The reason I ask is not to change the method in about 50 routes.
let sqlString = `select * from table where userid=${userId}`
Sanitizing code here
let conn = await sql.connect(process.env.DB_CONNSTRING)
let recordset = await conn.query(sqlString)
Thanks.
According to https://tediousjs.github.io/node-mssql/ , "All values are automatically sanitized against sql injection." applies only when you use the ES6 Tagged template literals. You should add the tag sql.query before the template string.
let sqlString = sql.query`select * from mytable where id = ${value}`
For more information on tagged template literals: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates

pyodbc - add column to MS Access with default value

I'm trying to add a column to an MS Access database table using pyodbc and Python 3.5.
Using the expression
self.cursor.execute("ALTER TABLE data ADD COLUMN testColumn TEXT(10)")
works fine, but when I try to add a default value (DEFAULT "no"), it throws a Syntax error. I've tried multiple combinations, but no luck.
Any help is much appreciated!
Cheers
Sadly, the Access ODBC driver simply does not support the DEFAULT clause for a column in CREATE/ALTER TABLE statements. The ODBC driver and OLEDB provider for Access have diverged somewhat in their DDL support, so unfortunately we can get inconsistent results for the same DDL statement as illustrated by the following VBScript code using ADO:
OLEDB works fine ...
Option Explicit
Dim conn
Set conn = CreateObject("ADODB.Connection")
Dim connStr
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Public\mdbTest.mdb"
conn.Open connStr
On Error Resume Next
conn.Execute "DROP TABLE Cheeses"
On Error GoTo 0
conn.Execute "CREATE TABLE Cheeses (Id LONG PRIMARY KEY, CheeseName TEXT(50) DEFAULT 'Cheddar')"
conn.Execute "INSERT INTO Cheeses (Id) VALUES (1)"
Dim rst
Set rst = CreateObject("ADODB.Recordset")
rst.Open "SELECT CheeseName FROM Cheeses WHERE Id = 1", conn
If rst("CheeseName").Value = "Cheddar" Then
WScript.Echo "Success"
End If
conn.Close
... but if we change the connection string to use ODBC ...
connStr = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\Users\Public\mdbTest.mdb"
... then our attempt to execute the CREATE TABLE statement fails with
Microsoft OLE DB Provider for ODBC Drivers: [Microsoft][ODBC Microsoft Access Driver] Syntax error in CREATE TABLE statement.
TL;DR - You can't use the DEFAULT clause in a CREATE/ALTER TABLE statement under pyodbc.

Trying to input user information to 2 tables at once sql

I am trying to add data to 2 tables at the same time via user input, however all its doing is inserting into the first table and not the 2nd.The code is for an azure sql database
String query = "BEGIN TRANSACTION;
INSERT INTO cc_customer (customer_id,customer_first_name,customer_surname,customer_tel_number,customer_cell_number,customer_status,employee_number)
VALUES ('"+id.toString()+"','"+ name.toString()+"','"+ Lname.toString()+"','"+ Telnum.toString()+"','"+Cellnum.toString()+"','"+Status.toString()+"','"+Empnum.toString()+"');
"+"
INSERT INTO cc_customer_address (customer_address_id,customer_building_number,customer_street,customer_suburb,customer_city,customer_zip_code)
VALUES ('"+Cusnum.toString()+"','"+ Cusbuild.toString()+"','"+ Cusstr.toString()+"','"+ Cussub.toString()+"','"+Cuscity.toString()+"','"+Cuszip.toString()+"')COMMIT;";
this worked!!!!
String query = "BEGIN TRANSACTION INSERT INTO cc_customer(customer_id,customer_first_name,customer_surname,customer_tel_number,customer_cell_number,customer_status,employee_number) VALUES ('"+id.toString()+"','"+name.toString()+"','"+Lname.toString()+"','"+Telnum.toString()+"','"+Cellnum.toString()+"','"+Status.toString()+"','"+Empnum.toString()+"')"+"INSERT INTO cc_customer_address(customer_number,customer_building_number,customer_street,customer_suburb,customer_city,customer_zip_code) VALUES ('"+Cusnum.toString()+"','"+Cusbuild.toString()+"','"+Cusstr.toString()+"','"+Cussub.toString()+"','"+Cuscity.toString()+"','"+Cuszip.toString()+"') COMMIT";

Can't retrieve the comment for columns but not table when using SchemaCrawler

I am using SchemaCrawler to get the metadata for MySQL5.7 tables using the following code:
final Connection connection = ...;
final DatabaseSpecificOverrideOptions databaseSpecificOverrideOptions =
SchemaCrawlerUtility.matchDatabaseSpecificOverrideOptions(connection);
final SchemaCrawler schemaCrawler = new SchemaCrawler(connection, databaseSpecificOverrideOptions);
final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
options.setSchemaInfoLevel(SchemaInfoLevelBuilder.maximum());
options.setTableInclusionRule(new IncludeAll());
options.setColumnInclusionRule(new IncludeAll());
final Catalog catalog = schemaCrawler.crawl(options);
final Collection<Table> tables = catalog.getTables();
for (Table t : tables) {
logger.info("Table comment: {}", t.getRemarks());
}
This is the test table:
create table testtable (
id bigint comment 'key level comment',
name varchar(32) comment 'column level comment'
) comment='table level comment';
I can get the column-level comments, but I never can get the table-level comment.
Is there anything that I mis-configured ?
Thanks!
This is an annoyance with the MySQL JDBC driver. You need to set useInformationSchema=true in your JDBC connection URL when creating a connection. For more information, please take a look at the StackOverflow question, Retrieve mysql table comment using DatabaseMetaData.
Sualeh Fatehi, SchemaCrawler

Passing multiple rows to SQL Server Stored Procedure

I have a problem that I want to pass multiple rows of Excel file to a SQL Server stored procedure and I don't have any idea about it.
I have an Excel file to be imported, data needs to be inserted into three different tables. I have successfully imported data into one table as follows
string path = string.Concat((Server.MapPath("~/temp/"+FileUpload1.FileName)));
FileUpload1.PostedFile.SaveAs(path);
OleDbConnection oleCon = new OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + path + ";Extended Properties = Excel 12.0;");
OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]",oleCon);
OleDbDataAdapter dtap = new OleDbDataAdapter(cmd);
oleCon.Open();
DbDataReader rdr = cmd.ExecuteReader();
string con_str = #"Data source = .;Initial Catalog=practiceDB;Integrated security=True";
SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
bulkInsert.DestinationTableName = "Excel_New";
bulkInsert.WriteToServer(rdr);
oleCon.Close();
Array.ForEach(Directory.GetFiles((Server.MapPath("~/temp/"))),File.Delete);
How to send rows to stored procedure rather than to any DestinationTable?
Rather than passing the rows as parameters, how about just reading from your table Excel_New the in the stored procedure?

Resources