Thanks for the help in advance
I am using access 2013 with a wpf vb.net app at the moment I have
Public sConnstring As String
Function Conn_DB1()
'<<<<<<<<<<<<<< This is for Data Base 1 >>>>>>>>>>>>>>>
sConnstring = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= " & My.Settings.DatastoreLocation & "\ACCDB\DB1.accdb; Persist Security Info=False;"
Return sConnstring
End Function
However as I want to password protect the database if I use the old
sConnstring = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= " & My.Settings.DatastoreLocation & "\ACCDB\DB1.accdb; Jet OLEDB:Database Password=MyDbPassword;
I can not seem to get it to connect I get the error
"Cannot Open Database - It may not be that the database that your application recognizes, or that the file may be corrupt"
Any Ideas as to why this may be happening?
Cheers
Related
i face problem when i try create sqlite database with password.
When I create database without password it is work very good but if i try to set password it give me error message "Could not load file or assembly 'System.Data.SQLite.SEE.License, Version=1.0.117.0, Culture=neutral, PublicKeyToken=433d9874d0bb98c5' or one of its dependencies. The system cannot find the file specified.".
My Code :
My Code Is :
Imports System.Data.SQLite
Imports System.IO
Public Class Form1
Dim DB_Path As String = Application.StartupPath & "\DB\database.sqlite"
Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
If File.Exists(DB_Path) = False Then
' create db folder if not exist
If Not (System.IO.Directory.Exists(Application.StartupPath & "\DB\")) Then
System.IO.Directory.CreateDirectory(Application.StartupPath & "\DB\")
End If
' Create a new SQLite connection
Using conn As New SQLiteConnection("Data Source=" & Application.StartupPath & "\DB\database.sqlite" & ";Version=3;")
'set db password
conn.SetPassword("ashraf123") ' the error here
'change db conn string
conn.ConnectionString = "Data Source=" & DB_Path & ";Password=ashraf123;Version=3;"
'open conn
Await conn.OpenAsync
' do somthing
End Using
Else
Using conn As New SQLiteConnection("Data Source=" & DB_Path & ";Password=ashraf123;Version=3;")
Await conn.OpenAsync
' do somthing
End Using
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
I tried that change platform from any cpu to x86.
I tried that change platform from any cpu to x64.
I tried that Copy system.data.sqlite.dll and all other .dll files to startup app folder.
I tried uninstall all packages and reinstalled them again.
i tried creat new project and do all mentioned steps.
install this package SQLitePCLRaw.bundle_e_sqlcipher
and use the password in connection string like this :
Data Source=" & DB_Path & ";Password=ashraf123
I want to change my .mdb Database password by C# code. I am using following code for this but some error comes. So please help me.
IErrorInfo.GetDescription failed with E_FAIL(0x80004005). This error comes.
Code:
OleDbConnection cnn1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Jet OLEDB:Database Password=" + pwd + ";Mode=Share Exclusive");
OleDbCommand cmd1 = new OleDbCommand();
cnn1.Open();
cmd1.Connection = cnn1;
string Query="ALTER DATABASE PASSWORD <newPassword> " + pwd + "";
cmd1.CommandText = Query;
blnSuccess = cmd1.ExecuteNonQuery();
In the Last finally i got the answer of my Question.
Following code is help me for change the .mdb Database Password change by C# Code.
Add a reference to Microsoft DAO 3.6 Object Library
using DAO;
public void ChangePassword(string sDBPath, string sDBPasswordOld, string sDBPasswordNew)
{
dao.DBEngine dbEngine;
dao.Database db;
db = dbEngine.OpenDatabase(sDBPath, true, false, ";PWD=" + sDBPasswordOld);
db.NewPassword(sDBPasswordOld, sDBPasswordNew);
}
I'm using VB.NET to create a simple application which will test if a variety of SQL Server are available online.
I have the code below, but the timeout is not working and it simply waits forever rather than throwing a timeout error. I have put breakpoints in and, as this is in an loop of IP's, it never progresses if the IP being checked is unavailable.
Dim data As New SqlClient.SqlConnection("Data Source=DatabaseIP;Initial Catalog=POS;Integrated Security=False;User ID=sa;Password=;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False".Replace("DatabaseIP", IP))
Try
data.Open()
Catch ex As Exception
Dim stophere As String = ""
TextBox1.Text += IP + vbNewLine
End Try
Connect Timeout=15 is not correct, try it with Connection Timeout=15
To connect with Database, following code is enough,
Dim data As New SqlClient.SqlConnection("Data Source=DatabaseIP;Initial Catalog=POS;Integrated Security=False;User ID=sa;Password=;")
I'm trying to connect to a SQL Server database that is not local. I have the Data Source and Initial Catalog - no issues. But need to change Integrated Security to False and insert SQL Server credentials.
Does anyone have any idea how put that in the connection string?
Also, does anyone know how to handle SecureStrings?
Here is my code so far:
Dim pwd As New SecureString("Password")
Dim cred As New SqlCredential("Username", pwd)
Dim sql As New SqlConnection("Data Source=OnlineServer;Initial Catalog=DatabaseName;Integrated Security=False")
Have a look at here: SQL Connection Strings to hopefully find which one you need. This will give you the basics.
To make the SQL account credentials confidential, you should encrypt the <connection strings> section in the web.config. to do so:
aspnet_regiis -pe "connectionStrings" -app "OnlineServer" -prov "DataProtectionConfigurationProvider"
Retrieving your connection string using ConfigurationManager will automatically decrypt the string
Dim connectionString = ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString
Here is a Microsoft Link that explains it further.
I worked out what I needed to do and how to handle secure strings.
Here is a code snippet for anyone who struggles in the future:
Imports System.Data.SqlClient
Imports System.Net.Mail
Imports System.Security
Public Module secure
Public Function sql()
Dim pass As String = "Password"
Dim pwd As SecureString = New SecureString()
For Each ch As Char In pass
pwd.AppendChar(ch)
Next
pwd.MakeReadOnly()
Dim cred As New SqlCredential("SQL_Login", pwd)
Dim conn As New SqlConnection("Server=Database_Name;Initial Catalog=Database_Address;Integrated Security=False", cred)
Return conn
End Function
End Module
Public Class sqlCommunications
Dim sql As New SqlConnection
Dim sqlcom As New SqlCommand
Public Sub start()
sql = secure.sql
sqlcom.Connection = sql
sql.Open()
sql.Close()
End Sub
End Class
In .NET I simply use Application Name = MyApp inside the connection string, but when using ADO connection through VBA the Activity Monitor of the SQL Server Management Studio always shows Microsoft Office 2010 in Processes on the Application column no matter what name I set on the VBA code.
conn.ConnectionString = "UID=" & UID & ";PWD=" & PWD & ";DSN=" & DSN & _
";Application Name = MyApp"
How can I set the application name for monitoring purposes?
Ahh I see VBA connection string doesn't support the Application Name attribute. It simply isn't being recognized when used within VBA. The only way I can think of solving this at the moment it's to return an ADODB.Connection object from a COM C# library.
Your own COM library would return an ADODB.Connection object with a predefined connection string which seem to work in .NET. You will be connecting to the database using a VBA ADODB.Connection object but with a substituted object reference. Instead of
Set cn = new ADODB.Connection you will use a GetConection() method exposed by your own library.
Dim cn as ADODB.Connection
Set cn = yourCOMlibrary.GetConnection
here are the steps
Download and install Visual Studio Express for Windows (FREE)
Open it as Administrator and create a New Project. Select Visual C# then Class Library and rename it to MyConnection
In the Solution Explorer, rename Class1.cs to ServerConnection.cs
Right click your MyConnection project in the Solution Explorer and select Add Reference
Type activeX in the search box and tick the Microsoft ActiveX Data Objects 6.1 Library
Copy and paste the below code into the ServerConnection.cs completely replacing whatever is in the file.
using System;
using System.Runtime.InteropServices;
using System.IO;
using ADODB;
namespace MyConnection
{
[InterfaceType(ComInterfaceType.InterfaceIsDual),
Guid("32A5A235-DA9F-47F0-B02C-9243315F55FD")]
public interface INetConnection
{
Connection GetConnection();
void Dispose();
}
[ClassInterface(ClassInterfaceType.None)]
[Guid("4E7C6DA2-2606-4100-97BB-AB11D85E54A3")]
public class ServerConnection : INetConnection, IDisposable
{
private Connection cn;
private string cnStr = "Provider=SQLOLEDB; Data Source=SERVER\\DB; Initial Catalog=default_catalog; User ID=username; Password=password;Application Name=MyNetConnection";
public Connection GetConnection()
{
cn = new Connection();
cn.ConnectionString = cnStr;
return cn;
}
public void Dispose()
{
cn = null;
GC.Collect();
}
}
}
Locate the cnStr variable in the code and UPDATE your connection string details.
Note: if you are unsure about the connection string you should use see ALL CONNECTION STRINGS
Click on TOOLs in Visual Studio and CREATE GUID
Replace the GUIDs with your own and remove the curly braces so they are in the same format as the ones you see now from the copied code
Right click MyConnection in the Solution Explorer and select Properties.
Click the Application tab on the left side, then Assembly Info and tick Make Assembly COM-Visible
Click the *Build* from the menu on the left and tick Register For COM Interop
Note: If you are developing for 64-bit Office then make sure you change the Platform Target on the Build menu to x64! This is mandatory for 64-bit Office COM libraries to avoid any ActiveX related errors.
Right click MyConnection in the Solution Explorer and select Build from the menu.
If everything went OK then your MyConnection.dll and MyConnection.tlb should be successfully generated. Go to this path now
C:\Users\username\desktop\
or wherever you saved them
and you should see your files.
Now open Excel and go to VBE. Click Tools and select References.
Click the Browse button and navigate to the MyConnection.tlb.
Also, add references to Microsoft ActiveX Object 6.1 Library - this is so you can use ADODB library.
Now right click anywhere in the Project Explorer window and Insert a new Module
copy and paste the below code to it
Option Explicit
Sub Main()
Dim myNetConnection As ServerConnection
Set myNetConnection = New ServerConnection
Dim cn As ADODB.Connection
Set cn = myNetConnection.GetConnection
cn.Open
Application.Wait (Now + TimeValue("0:00:10"))
cn.Close
Set cn = Nothing
myNetConnection.Dispose
End Sub
Open SQL Server Management Studio, right click the server and select Activity Monitor
dont close this window
Go back to Excel and hit F5 or hit the green play button on the ribbon.
now switch back to SSMS ( SQL Server Management Studio )
and wait for your custom connection name to appear! :)
Here we go! That was easy, wasn't it? :)
This is what is happening.
You are returning an ADODB Connection object from you C# COM library by using myNetConnection.GetConnection function
Dim myNetConnection As ServerConnection
Set myNetConnection = New ServerConnection
Dim cn As ADODB.Connection
Set cn = myNetConnection.GetConnection
It's almost like saying Set cn = new ADODB.Connection but with predefined connection string which you did in your C# code.
You can use the cn object like a normal ADODB.Connection object within VBA now.
Remember to always .Close() the ADODB.Connection. A good programmers practice is to always close anything you open - streams, connections, etc.
You can rely on the Garbage Collector to free references/ memory but I also wrote a Dispose() method for you so you can force the GC to run. You can do that to immediately get rid of the Connection so it does not hang in the SSMS as opened.
Remember to use myNetConnection.Dispose along with the cn.Close and you'll be fine.
Note:
This is how I would do it if any one thinks this is wrong or needs to be updates (as being unstable or unsafe) please leave a comment.
Well, I hope this will be helpful to anyone in the future :)
The correct keyword to set the application name in an ADODB connection string in VBA is APP, not Application Name.
Example connection string, copied from an MS Access app I'm working on:
DRIVER={SQL Server};SERVER=xxxx;DATABASE=xxxx;Trusted_Connection=Yes;APP=xxxx