Export SQL database to Access - ASP.NET - asp.net

Is there any way to export data (not necessarily schema) to an access database via asp.net?
The server has no office components installed and the process must occur via a webpage (like an excel export).

You have to do it programatically.
Open the source table
Create a new AccessDB using ADO Extensions (as shown above)
Create the table in the AccessDB by reading the source schema (CREATE TABLE X ...)
Iterate thought the source table inserting the records in the Access table
Note: Code from http://www.freevbcode.com/ShowCode.asp?ID=5797 posted here in case the link cease to exists in the future
'select References from the Project Menu, choose the COM tab,
'and add a reference to Microsoft ADO Ext. 2.7 for DDL and Security
Public Function CreateAccessDatabase( ByVal DatabaseFullPath As String) As Boolean
Dim bAns As Boolean
Dim cat As New ADOX.Catalog()
Try
'Make sure the folder
'provided in the path exists. If file name w/o path
'is specified, the database will be created in your
'application folder.
Dim sCreateString As String
sCreateString = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
DatabaseFullPath
cat.Create(sCreateString)
bAns = True
Catch Excep As System.Runtime.InteropServices.COMException
bAns = False
'do whatever else you need to do here, log,
'msgbox etc.
Finally
cat = Nothing
End Try
Return bAns
End Function
DEMO
====
' If CreateAccessDatabase("F:\test.mdb") = True Then
' MsgBox("Database Created")
' Else
' MsgBox("Database Creation Failed")
' End If

Here is a very detailed article. It is something I stumbled upon, not an approach I am familiar with:
File Uploading to Access Database using ASP.NET
by Faisal Khan.

Related

Cannot make Access 2010 accde from Access 2007 accdb

I have a bit of a problem and could really use some help. My organization recently migrated from Office 2007 to Office 2010. I had a database that I developed using Access 2007 (using the .accdb database file type). Throughout the migration process, I was still making updates to my database. All the updates were made via an Office 2007 machine and everything worked on the 2010 systems that I deployed it to, as well as the 2007 boxes. The problem now is that since all the computers are officially on 2010, I cannot seem to create an Accde file from Access 2010. The error I receive is: " The command or action 'MakeMDEFile' isn't available now." * You may be in a read-only database or an unconverted database from an earlier version..." The code is compiled with no errors and my references are good. I have tried to re-compile the code, re-name the wizards in the "C:\Program Files\Microsoft Office\Office14\ACCWIZ" folder and let them re-install, and import all my objects into a new database based on this article: http://msdn.microsoft.com/library/office/dn602608%28v=office.14%29.aspx; all to no avail.I do not have any web content or anything Access 2010 specific, as I only made adjustments to the things I created in 2007. I did read that Access must be compiled on the same version that it was created on, but I thought since both 2007 and 2010 use the .accdb file format it would be compatible? Any advice on this? Thank you.
Thank you for your quick answer! That worked great. I was able to successfully export my DB using the code and I also imported all the objects with the exception of the queries. (I was able to create the accde.) Because I have so many objects, I used a script to import everything. The problem I am experiencing now is with my SQL queries. The export script named the text files a little different for the SQL queries and I don't know how to handle them. Below is my code that worked for the rest of the objects:
Public Sub batchImport_queries()
On Error GoTo batchImport_Err
Dim objFS As Object, objFolder As Object
Dim objFiles As Object, objF1 As Object
Dim strFolderPath As String
strFolderPath = "C:\Users\Me\Desktop\dbexport\queries\"
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFS.GetFolder(strFolderPath)
Set objFiles = objFolder.files
For Each objF1 In objFiles
objF1.Name = Right(objF1.Name, Len(objF1.Name) - 6)'strips "Query_"
objF1.Name = Left(objF1.Name, Len(objF1.Name) - 3) 'strips ".txt"
Application.Application.LoadFromText acQuery, objF1.Name, strFolderPath & objF1.Name
Next
Set objF1 = Nothing
Set objFiles = Nothing
Set objFolder = Nothing
Set objFS = Nothing
batchImport_Exit:
Exit Sub
batchImport_Err:
MsgBox Err.Number & " " & Err.Description
Resume batchImport_Exit
End Sub
That worked for queries like: "Query_qryAvailable.txt" but the SQL ones look like this: "Query_~sq_cCIPSSubform~sq_RosterSubform.txt". It seems to be encapsulating "~sq_c" around the first part of the query name and then the form/subform/or control that is associated with it at the last part of the filename...or I could be completely off. I can't figure out the pattern. Some of them have "~sq_f" instead, only at the leading part.(I'm guessing those are for forms?) Anyway, is there a better way to format the file name (if that's what has to be done) to remove those to my original query names and import correctly? Please let me know if that doesn't make sense. Thank you for your time.
It's possible to export an Access database to text files, see here: http://www.access-programmers.co.uk/forums/showthread.php?t=99179.
Option Compare Database
Option Explicit
Public Sub ExportDatabaseObjects()
On Error GoTo Err_ExportDatabaseObjects
Dim db As Database
'Dim db As DAO.Database
Dim td As TableDef
Dim d As Document
Dim c As Container
Dim i As Integer
Dim sExportLocation As String
Set db = CurrentDb()
sExportLocation = "C:\Temp\" 'Do not forget the closing back slash! ie: C:\Temp\
For Each td In db.TableDefs 'Tables
If Left(td.Name, 4) <> "MSys" Then
DoCmd.TransferText acExportDelim, , td.Name, sExportLocation & "Table_" & td.Name & ".txt", True
End If
Next td
Set c = db.Containers("Forms")
For Each d In c.Documents
Application.SaveAsText acForm, d.Name, sExportLocation & "Form_" & d.Name & ".txt"
Next d
Set c = db.Containers("Reports")
For Each d In c.Documents
Application.SaveAsText acReport, d.Name, sExportLocation & "Report_" & d.Name & ".txt"
Next d
Set c = db.Containers("Scripts")
For Each d In c.Documents
Application.SaveAsText acMacro, d.Name, sExportLocation & "Macro_" & d.Name & ".txt"
Next d
Set c = db.Containers("Modules")
For Each d In c.Documents
Application.SaveAsText acModule, d.Name, sExportLocation & "Module_" & d.Name & ".txt"
Next d
For i = 0 To db.QueryDefs.Count - 1
Application.SaveAsText acQuery, db.QueryDefs(i).Name, sExportLocation & "Query_" & db.QueryDefs(i).Name & ".txt"
Next i
Set db = Nothing
Set c = Nothing
MsgBox "All database objects have been exported as a text file to " & sExportLocation, vbInformation
Exit_ExportDatabaseObjects:
Exit Sub
Err_ExportDatabaseObjects:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_ExportDatabaseObjects
End Sub
If the principle still holds, you should be able to import the resulting objects to a fresh database. If there are any problems with permissions, that should become evident in the text files, but normally this strips all permissions.

ADODB Recordset Field Value Returns Incorrectly?

I'm currently troubleshooting some old code that simply serves up a file from a file share, and can't figure out why the SQL query is not returning what I expect from an Oracle database.
Here you can see the database entry for the file I'm trying to download:
And here is what the below javascript alert returns:
You can see that the Fields(index).value call below is returning the above value. It seems to be escaping the backslashes, how can I resolve?
dim goCon
set goCon = Server.CreateObject("ADODB.Connection")
dim gsSQL
'Define SQL here to query database and return frtFileName
set goRSet = goCon.Execute(gsSQL)
if goRSet.EOF then
Err.Raise 1,Request.ServerVariables("URL"), "Invaild Report ID"
else
gsFileName = goRSet.Fields("frtFileName").value
Response.Write "<script type='text/javascript'>alert('gsFileName:" & gsFileName & "');</script>"

User details stored in separate table ASP.NET Identity

I am a complete beginner at ASP.net(and this forum) i am using Visual studio 2013 and have created created another table in the created database using the package manager console.
How do i go about placing the information into this new table? (I am looking to store firstname and last name in a separate table)
The create account button is below:
Protected Sub CreateUser_Click(sender As Object, e As EventArgs)
Dim userName As String = UserNameCtrl.Text
Dim Firstnane As String = firstnamectrl.Text
Dim manager = New UserManager
Dim User = New ApplicationUser() With {.UserName = userName}
Dim result = manager.Create(User, Password.Text)
If result.Succeeded Then
IdentityHelper.SignIn(manager, User, isPersistent:=False)
IdentityHelper.RedirectToReturnUrl(Request.QueryString("ReturnUrl"), Response)
Else
ErrorMessage.Text = result.Errors.FirstOrDefault()
End If
End Sub
Any pointers in the right direction, hints or suggested reading would be very helpful.
If I understand correctly, this link may be of some help:
http://www.codeguru.com/vb/gen/vb_database/adonet/article.php/c15033/A-Basic-VBNET-ADONET-Tutorial-Adding-Deleting-and-Updating.htm
It is for a windows form application, but it should translate pretty well if you're using web forms. Basically, you just want to make a connection to the database during the button click event (the simplest way I know of to make this connection is using ADO.NET), and pass the values of the first and last name in a SQL query to the sql server.
You would be building the sql query as a string, and concatenating your vb variables into that string. Something like; "Insert into table xxx(firstname, LastName) values " & Firstname & ", " & Lastname...

Start an Instance of Autodesk Inventor

Am Using Inventor api for customizing inventor documents.Here I use vb.net code for start an instance of the Inventor .my code is
inventorApp = CreateObject("Inventor.Application", "")
inventorApp.Visible = True
it is ok and working fine .but when we open the visual studio run as administrator then the createobject having some error.Any one know any other way to start an instance of Inventor?
Try using the marshal method instead.
Dim m_inventorApp As Inventor.Application
Try ' Try to use active inventor instance
Try
m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
m_inventorApp.SilentOperation = True
Catch ' If not active, create a new instance of Inventor
Dim inventorAppType As Type = System.Type.GetTypeFromProgID("Inventor.Application")
m_inventorApp = System.Activator.CreateInstance(inventorAppType)
' Must set visible explicitly
m_inventorApp.Visible = True
m_inventorApp.SilentOperation = True
End Try
Catch
'Cant get or create an instance of inventor.
End Try
Private Sub Open_Button_Click()
ThisApplication.SilentOperation = True 'Suppresses the resolve links dialog
Dim myPath As String
myPath = FileName.Text 'Gets the string, FileName, from module 1
Dim Shell As Object
Set Shell = CreateObject("Shell.Application")
Shell.Open (myPath) 'Opens selected file
Resolve_and_Open.Hide 'Hides module
CompareStrings
End Sub
This is to open a sad assembly that needs to resolve links. I'm not sure if this will get around that error, but try using this:
ThisApplication.SilentOperation = True
Either that, or creating a shell and then opening it that way instead of directly.

SQLite file location in a Visual Studio project

I have a small application I'm working on and part of the project's goals is simply to get more familiar with SQLite. The main entry point is a console app (sub-project named Console), but the data access code is in a different sub-project (named Infrastructure). I just created a folder in Infrastructure named Data and dropped the .sqlite file there and added some dummy tables and data.
The project looks like this:
Solution
Console (project)
Domain (project)
Infrastructure (project)
Data (folder)
My original connection string just looked like this:
var cnx = new SQLiteConnection("Data Source=testing.sqlite; FailIfMissing=True");
What I first saw happening was that a 0KB testing.sqlite was being dropped in the Debug directory. A simple count query would fail because the table I was referencing obviously didn't exist in the empty file. After some playing around, I found that if I set the properties on the .sqlite file to be Content and Copy Always and if I changed the connection string, I could connect as expected.
var cnx = new SQLiteConnection("Data Source=Data\\testing.sqlite; FailIfMissing=True");
Am I missing something here, or is this indeed how I should be including the file in my project?
I'm thinking the best way to go here is probably to move the file outside of the project and just use a setting in App.config.
In App.config:
<connectionStrings>
<add name="mydb" connectionString="Data Source=D:\\Data\\testing.sqlite; FailIfMissing=True"/>
</connectionStrings>
In code:
private string _connectionString = ConfigurationManager.ConnectionStrings["mydb"].ConnectionString;
If I want the physical file in my project I can continue to do what I noted above.
If the SQLite database does not need to be populated with much data before distribution, you can just create it on runtime. If you want it to be in the same location as your executable, try this to create it (VB.Net code)
Dim SQLconnect As New SQLite.SQLiteConnection()
SQLconnect.ConnectionString = "Data Source=" & Application.StartupPath & "\database.db;"
SQLconnect.Open()
SQLconnect.Close()
This will create a blank database file and if you want to create the tables and/or load some data, you could do something like this (after checking to see if the tables already exist of course):
If tblSettingsExist = False Then
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = "CREATE TABLE Settings(setting TEXT PRIMARY KEY, value TEXT);"
SQLcommand.ExecuteNonQuery()
SQLcommand.Dispose()
SQLconnect.Close()
'add records for all default settings
SQLite_InsertRecord("Settings", "setting, value", "'someSetting1', '-1'")
SQLite_InsertRecord("Settings", "setting, value", "'someSetting2', '0'")
SQLite_InsertRecord("Settings", "setting, value", "'someSetting3', '1'")
End If
Just for completeness, here is the subroutine I use to insert data:
Public Sub SQLite_InsertRecord(ByVal table As String, ByVal fields As String, ByVal values As String)
Dim SQLcommand As SQLiteCommand
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = "INSERT INTO " & table & " (" & fields & ") VALUES (" & values & ")"
Try
SQLcommand.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Database Insertion Error")
Finally
SQLcommand.Dispose()
SQLconnect.Close()
End Try
End Sub

Resources