I have an asp file which contains a connectionString to my database.
I'm wondering if there is a way to change that information before the user try to log on the system.
I named it _conn.asp and it contains the following asp code
dim conn
sub OpenConn()
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open = "Driver=driver_name; SERVER=server_name; uid=user_name; pwd=user_pwd; DATABASE=db_name;"
End sub
Sub CloseConn()
Conn.Close
Set Conn = Nothing
End sub
I want to be able to change the driver, server, uid, pwd and database information inside that asp file.
First I realized that an xml file would be the best choice but then I heard about a bunch of security problems involving putting a connectionString on a xml file.
If it is not possible to update the _conn.asp file, what would be the best practice to make an updatable file to hold the connectionString to a database in classic ASP?
Usually you would put the connection string into a separate file in the directory not listed in IIS. Say, your ASP pages in Website\Scripts folder. Create a new folder Website\Includes. make sure it is not listed. Create a file, say DSN.inc inside this folder.
Now, in your asp pages add this line:
<!-- #include FILE="../Includes/dsn.inc" -->
DSN.inc would contain the connection string (here is an example of Jet4.0 connection string)
set rs=Server.CreateObject("adodb.Recordset")
sDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath ("..\Databases\db.mdb") & "; "
In your ASP pages just re-use rs
Related
First I created a table in a SQL Server database with columns like document id, docname and docdata.
Second I managed to upload a document into that table using MS Access and ole object and now I have a word document there, by double click the ole object in the MS Access form interface I could open the document.
What is interesting about this is that I can edit the document and get the changes saved to the document inside the server.
The idea is that I want to use the SQL Server over our LAN-network as an alternative to OneDrive and make collaborative working possible to my coworkers with no internet service.
Now I am trying to open that document using using VBA in MS Word.
Here is my vague attempt:
Option Explicit
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub CommandButton1_Click()
' my own connection string
cn.Open "provider=sqloledb;server=127.0.0.1,1433;database=accessdb;UID=sa;PWD=111111"
With rs
Set .ActiveConnection = cn
.Source = "select * from docs where did=1"
.LockType = adLockOptimistic
.CursorType = adOpenKeyset
.CursorLocation = adUseClient
.Open
End With
Dim WordObj As Word.Application
'rs![docdata].Verb = -2
'rs![docdata].Action = 7 ' Activates the application.
Dim obdoc As Word.Document
Set obdoc = rs![docdata] ' what property to attach here ??
obdoc.SaveAs2 "D:\thisdoc.doc"
WordObj.Documents.Open obdoc
WordObj.Activate
End Sub
A mismatch error pops up and I did not know how to deal with this, what type of property should retrieve the data from that field and assign it to the newly made document.
Update 20-2-2020
Um, Now I recognized that the Ms-word document isn't a simple RTF file.
It is a zipped folder ! see this !
and in order to verify this I removed the .docx extension and replaced it with .zip,
then I unzipped it and got some folders inside of them a few .xml files..
This means that the upper approach is not compatible with the nature of this file. type.
I have an SQL query in Excel. After the query a message should appear. This works well as an existing user, but for new users the password query is hidden.
I was looking for an alternative to Application.CalculateUntilAsyncQueriesDone but couldn't find a solution. The Excel login mask is always not displayed.
ThisWorkbook.RefreshAll
Application.CalculateUntilAsyncQueriesDone
MsgBox "The new data has been imported.", , "Import done"
How can I get Excel to wait for the end of the query and get the login displayed before (if I have never entered the password as a new user)?
Ok, so this not the solution you're looking for, but I don't think it exists I'm afraid. Embedded query objects in Excel are horrible to work with, and they expect you to be using Active Directory for your SQL Server password, hence the exceptionally poor handling of the "login" popup box.
My advice would be to move away from embedded workbook tables / queries and script out your queries using an ADODB connection, purely in VBA.
If the user's password is a simple username / password combo, then you can read it from a cell in your sheet before you send the query to the server.
If the login is based on Active Directory, then the user doesn't need to enter their password at all, ADODB will just log them on automatically.
Here's my simple SQL function I use to query SQL Server databases using ADODB. The long string in the middle after "conn.open" is the connection string. In that, replace "DatabaseHostName" with the Host name of your DB, and "DatabaseName" with the name of the DB you want to query by default:
Sub ReadSQL(SQLQuery As String, DestinationRecordSet As Object)
Dim conn As Object
Set conn = CreateObject("ADODB.Connection")
Set DestinationRecordSet = CreateObject("ADODB.Recordset")
conn.Open "Provider=SQLOLEDB;Data Source=DatabaseHostName;Initial Catalog=DatabaseName;Integrated Security=SSPI;"
conn.CommandTimeout = 0
Set DestinationRecordSet = conn.Execute(SQLQuery)
End Sub
then I call it like this:
Sub GetData()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim OutputRecordSet As Object
Dim SQLQuery As String
SQLQuery = "SELECT TOP 10 * FROM dbo.SomeTable"
ReadSQL SQLQuery, OutputRecordSet
'Clear output sheet'
wb.Sheets("Output").Cells.ClearContents
'Write Headers from query in row 1'
For x = 0 To OutputRecordSet.Fields.Count - 1
wb.Sheets("Output").Cells(1, x + 1).Value = OutputRecordSet.Fields(x).Name
Next x
'Copy the data from the query to the worksheet, under the headers'
wb.Sheets("Output").Range("A2").CopyFromRecordset OutputRecordSet
End Sub
When I call my "ReadSQL" sub from this sub, by default it waits until the query is complete before doing anything else.
Again I would strongly recommend active directory logins for your DB, then you never have to worry about passwords, but If you wanted to embed a user's password into the connection string, you could, or you could build the connection string based on something the user entered, or had entered into a cell in the workbook.
In any case, you will find once you move away from Excel's embedded tables which work with the dreaded "Refresh All" button, things will become much easier to control, and your queries will run about 10 times faster.
I have many Access database files that contain linked tables to a SQLServer database. Now the database server has changed and I have to edit the links, possibly without recreating them.
it's possible to do it? I use Access 2013.
Yes it is possible to do with VBA but the way you'll do it really depends on how you linked the tables.
Here are 2 example of connection strings I use for SQL server tables
Direct connection:
Driver=SQL Server Native Client 10.0;Server=server_name;Address=server_address,1433;Network=network_name;Database=database_name;Trusted_Connection=Yes
DSN connection (with an entry in the ODBC control panel)
ODBC;Provider=SQLNCLI10;Server=server_name;DSN=name_of_DSN_entry_in_ODBC_control_panel;Database=database_name;Trusted_Connection=Yes
So the first thing to do is to determine how your tables are linked.
You can use this code (pay attention to the comments):
Public Sub Check_ODBC_tables()
Dim tdef As TableDef
' First loop on all tables to determine the connection strings
For Each tdef In CurrentDb.TableDefs
' only print the constring if the database name is your old database (adapt accordingly)
If InStr(tdef.Connect, "Database=old_database_name") Then
Debug.Print tdef.Connect
End If
Next
End Sub
Run this code (F5) and check the output in the immediate window. You'll find how the table are linked and what are the connection strings.
You should prepare a connection string based on that, and adapt in them the database name to use your new DB.
Once you are ready, you can adapt and run the following code that will delete the old table and recreate them with the new database names (some tweaks might be necessary), so go thought it in debug mode !
Public Sub Change_ODBC_tables()
Dim tDef As TableDef
Dim tDefNew As TableDef
Dim strTable As String
Dim strCOnString As String
' examples of constrings - ADAPT!!!!
strCOnString = "Driver=SQL Server Native Client 10.0;Server=server_name;Address=server_address,1433;Network=network_name;Database=database_name;Trusted_Connection=Yes"
strCOnString = "ODBC;Provider=SQLNCLI10;Server=server_name;DSN=name_of_DSN_entry_in_ODBC_control_panel;Database=database_name;Trusted_Connection=Yes"
For Each tDef In CurrentDb.TableDefs
If InStr(tDef.Connect, "Database=old_database_name") Then
' We find a match, store the table name
strTable = tDef.Name
' delete the linked table
DoCmd.DeleteObject acTable, strTable
' recreate the linked table with new DB name
Set tDef2 = CurrentDB.CreateTableDef(strTable)
tDef2.Connect = strCOnString
tDef2.SourceTableName = strTable
tDef2.Name = strTable
CurrentDb.TableDefs.Append tDef2
End If
Next
End Sub
If you don't fully understand the second piece of code I posted, I urge you to backup your mdb prior to run it because it will delete objects which can cause serious issues.
I have created a table in my Access front end application, and try to connect it to the back end database with the following subroutine:
Sub createFlowTable()
Dim db As Database
Dim tblDef As TableDef
Set db = CurrentDb
Set tblDef = db.CreateTableDef("myTable")
tblDef.Connect = "ODBC;DRIVER=SQL Server;SERVER=myServer; Trusted_Connection=No;UID=<myUID>;PWD=<myPWD>;APP=2007 Microsoft Office system;DATABASE=myDataBase;Network=DBMSSOCN;TABLE=dbo.myTable"
tblDef.SourceTableName = "mySourceTableName"
db.TableDefs.Append tblDef
End Sub
After I close the front end Access database, and upon reopening it, the table fails to open. Even though I have set the Trusted_Connection to "No" in my string, the table still tries to use the Windows Authentication. Also, when I open the table on design view, I see in front of "Description":
ODBC;DRIVER=SQL Server;Server=myServer;APP=2007 Microsoft Office System;DATABASE=myDatabase;Network=DBMSSOCN;Table=dbo.myTable
So obviously Access has not saved the UID and PWD, nor has it saved the instruction on setting the Trusted_Connection to "No".
I insist to get this done with the connection string, and using DSN will not work for the purpose of my application. Help would be greatly appreciated.
You need to add the dbAttachSavePWD-Attribute to the created table to store your credentials with the linked table in Access.
Before appending the tabledef you should put this line of code:
tblDef.Attributes = (tblDef.Attributes Or dbAttachSavePWD)
I want to create a SQL Server database at runtime in my vb.net project. I know how to actually code the database but I am wondering where should I actually put the code? Should I be putting the code in the start up form or should it go into a class on it's own? Also, this project will be going on more than one pc at a particular site, so I only want the database to be created the first time the project is activated and then just be able query the database on different pcs after that. How do I do this? All help on this matter would be greatly appreciated.
EDIT:
Ok, so I should have been clearer on this. The project is going to be on 2 different pcs, it is for visitors entering a business. The pcs will be in reception and security. I need both pcs to access the same database with the same details in it. I don't want to have two different databases where details have to be put in twice. For example, if I enter at reception today and then go through security tomorrow, then all I should have to enter in security is why I'm entering the business again, I shouldn't have to put my details in a second time. How do I go about this? As I already said, I know how to code the database, but I want to know how to do what I stated in my question.
Thanks in advance for all help given.
If you add the code in module or in form load then it will execute all the time when the form loads. it is wasting of time to check whether the database exist or not in each run. So it is better to place a button with text "Create database" for this purpose(or menu item). it's click event will load the database. the following code can be used to create the database dynamically on button click
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'creating and initializing the connection string
Dim myConnectionString As SqlConnection = New SqlConnection("Data Source=(local)\SQLEXPRESS;Initial Catalog=master;Integrated Security=True;Pooling=False")
'since we need to create a new database set the Initial Catalog as Master
'Which means we are creating database under master DB
Dim myCommand As String //to store the sql command to be executed
myCommand = "CREATE database my_db" //the command that creates new database
Dim cmd As SqlCommand = New SqlCommand(myCommand, myConnectionString) // creating command for execution
Try
cmd.Connection.Open() //open a connection with cmd
cmd.ExecuteNonQuery() //Execute the query
cmd.Connection.Close() //Close the connection
Catch
MsgBox(" Already installed database", MsgBoxStyle.Critical, " MaS InfoTech- Warning")
End Try
'Creating table to the dynamicaly created database
Try
Dim cn As SqlConnection = New SqlConnection("Data Source=(local)\SQLEXPRESS;Initial Catalog=my_db;Integrated Security=True;Pooling=False")
'here the connection string is initialized with Initial Catalog as my_db
Dim sql As String //sql query string
sql = "CREATE TABLE customer(cus_name varchar(50) NULL,address varchar(50) NULL,mobno numeric(18, 0) NULL,tin varchar(50) NULL,kg varchar(50) NULL)"
cmd = New SqlCommand(sql, cn) // create command with connection and query string
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
Catch
MsgBox(" Already existing table", MsgBoxStyle.Critical, " MaS InfoTech- Warning")
End Try
End Sub