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);
}
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 have a wpf application that is connected to my database which was created in microsoft SQL server Management using this:
private SqlConnection connect()
{
con = new SqlConnection();
con.ConnectionString = "Server=LAPTOP-1B0SDCU1 ;Database=project;User ID=sa;Password=123;Trusted_Connection=true;Integrated Security=false";
return con;
}
and added my data in a table called material as such
private void insertdata(object sender, RoutedEventArgs e)
{
connect();
con.Open();
string material = materialfield.Text;
string saved = "INSERT INTO materialtable(material) VALUES ('" + material + "')";
SqlCommand com = new SqlCommand(saved, con);
com.ExecuteReader();
viewdetails();
MessageBox.Show("record is added");
}
here my server is my own computer. Now I want to give this application to my client along with the database so that they can add details into the database and those details will only be stored on their laptop, but I do not know how to. Can someone please help.
I am working on created a login form for an application I am working on. I have my application set up to properly connect to the database and run stored procedures and queries on the database as well.
However I am unsure how to send messages from the database to my VB.Net Application. Right now I have essentially two methods that execute code for my database:
Public Function ExecuteCMD(ByRef CMD As SqlCommand) As DataTable
Dim DS As New DataSet()
Try
OpenDBConnection()
CMD.Connection = DB_CONNECTION
If CMD.CommandText.Contains(" ") Then
CMD.CommandType = CommandType.Text
Else
CMD.CommandType = CommandType.StoredProcedure
End If
Dim adapter As New SqlDataAdapter(CMD)
adapter.SelectCommand.CommandTimeout = 300
adapter.Fill(DS)
Catch ex As Exception
Throw New Exception("Database Error: " & ex.Message)
Finally
CloseDBConnection()
End Try
Return DS.Tables(0)
End Function
Public Function ExecuteCMDWithReturnValue(ByRef CMD As SqlCommand) As Boolean
Try
OpenDBConnection()
CMD.Connection = DB_CONNECTION
CMD.Parameters.Add("#ret", SqlDbType.Int).Direction = ParameterDirection.ReturnValue
CMD.CommandType = CommandType.StoredProcedure
CMD.ExecuteNonQuery()
Dim result As Object = CMD.Parameters("#ret").Value
Return If(Convert.ToInt32(result) = 1, False, True)
Catch ex As Exception
Throw New Exception("Database Error: " & ex.Message)
Return False
Finally
CloseDBConnection()
End Try
End Function
These functions honestly work fine, but they are horrible for error processing.
For example I'd like to be able to set up my Store Procedure for logging in to the application to return a "Username not found" or "Password incorrect" message so that I can display to my user exactly what the problem is, as opposed to just returning a generic "Login information incorrect" message from only returning true or false on the logging in going through or not.
I unfortunately do not know exactly how to do this on either end. I don't know what to set up on the SQL Server side to have it spit out messages in procedures, and I don't know hot to receive those messages in VB.Net.
You can verify your user right in VB. I don't think it is a good idea to tell the user If the password or user name is wrong (or if both are wrong). If this data is password protected then it should be protected from malicious logins. It would help a hacker to know what was wrong.
Private Function VerifyPassword(pword As String, uname As String) As Boolean
Using cn As New SqlConnection(My.Settings.UsersConnectionString)
Dim cmd As New SqlCommand("Select Count(*) From Users Where UserName = #UserName And UserPassword = #Password;", cn)
cmd.Parameters.Add("#UserName", SqlDbType.VarChar, 100).Value = uname
cmd.Parameters.Add("#Password", SqlDbType.VarChar, 100).Value = pword
Try
cn.Open()
Dim i As Integer = CInt(cmd.ExecuteScalar())
If i > 0 Then Return True
Return False
Catch ex As Exception
Throw
Finally
cn.Close()
cmd.Dispose()
End Try
End Using
End Function
Of course the password is stored hashed with a salt.
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
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