I have SQL Server 2016 LocalDB (.mdf file) and I want make backup of my database. I use the code shown below, but it does not work. I tested all code that exists in stackoverflow, but none of them worked.
Here is my code (this code works perfectly on SQL Server 2014 but does not work on localdb):
SqlConnection connect;
string con = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database\ClassSRM.mdf;Integrated Security=True";
connect = new SqlConnection(con);
connect.Open();
SqlCommand command = new SqlCommand(#"backup database " + Application.StartupPath + #"\Database\ClassSRM.mdf" + " to disk ='" + strFileName + "'", connect);
command.ExecuteNonQuery();
connect.Close();
Related
I'm writing a simple page in ASP.net to update a password in a Access table. Here's syntax for a SELECT query that works:
Dim dbConn As OleDbConnection
Dim dbCommand As OleDbCommand
Dim dbReader As OleDbDataReader
'Connect to db
dbConn = New OleDbConnection(Application("strConnectionString"))
dbConn.Open()
'Get user info
strSQL = "SELECT * FROM users WHERE Username = '" & strUsername & "';"
dbCommand = New OleDbCommand(strSQL, dbConn)
dbReader = dbCommand.ExecuteReader()
And my connection string:
Application("strConnectionString") = _
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strDBPath & _
"; Jet OLEDB:Database Password=" & strDBPassword & ";"
The SELECT works fine, so I know my connection is OK. But this query gives me a syntax error:
strSQL = "UPDATE users SET Password = '1'"
With everything else the same, the ASP error says there is an error with my syntax. But when I response.write the strSQL line, it gives me this:
UPDATE users SET Password = '1'
and when I paste this into a query editor in Access, the query updates all 'Password' field in the table to '1', so I know the syntax is OK. I tried it without the datareader and using dbCommand.ExecuteNonQuery(), same result.
I've got the permissions on the Access file set so that everybody has full control, so I don't think it's a permission problem.
Can anybody see my mistake? I'm really stuck. Thanks.
PASSWORD is a reserved word in Access SQL so you need to enclose it in square brackets if it is a column name.
strSQL = "UPDATE users SET [Password] = '1'"
I am connecting to the MS Access server with the following code.
Dim cnn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=162.222.225.78;Database=CRM.mdb;Integrated Security=SSPI;User ID=corpopef;Password=********;")
Dim cmd As New OleDb.OleDbCommand
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
cmd.Connection = cnn
cmd.CommandText = "INSERT INTO Addressbook(srno) " & _
"VALUES('" & Me.TextBox1.Text & "')"
cmd.ExecuteNonQuery()
But it results in the following error:
"Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done."
Please suggest to me some methods to work around this problem.
Thanks in advance...!
The connection string you used seems like an SQL client connection string. For an access database, you should use this format:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=path_to_mdb_file\CRM.MDB;User Id=user_id; Password=password;
I have a select into statement that works, but when i put it inside a Loop, it fails.
Does anyone have any idea why this is happening??
Thank you!
Here's the code:
For o = 0 To x
tables = ListBox1.Items.Item(o).ToString
sqlstr = "SELECT * into dbo." & tables & " FROM OPENROWSET('MSDASQL', 'Driver=Microsoft Visual FoxPro Driver; SourceDB=C:\exports\DBF; SourceType = DBF ', 'SELECT * FROM " & tables & "')"
mycmd = New SqlCommand
sqlcon = New SqlConnection(con)
mycmd.CommandText = sqlstr
sqlcon.Open()
mycmd.Connection = sqlcon
mycmd.ExecuteNonQuery()
sqlcon.Close()
Next
When i try to execute this it gives me following error
Cannot execute the query "SELECT * FROM AFED" against OLE DB provider "MSDASQL"
for linked server "(null)". OLE DB provider "MSDASQL"
for linked server "(null)" returned message
"[Microsoft][ODBC Visual FoxPro Driver]File 'afed.dbf' does not exist.".
Please check your foxpro db .. I think afed table does not exist in foxpro DB.and that is why you are not getting the error when put static value instead of o.
I have win application and i get this error when i want to connect the database to my app
error:
An attempt to attach an auto-named database for file C:\Users\Aren\Desktop\DB\REGISTRATION.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
public class DALBase
{
protected SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\\Users\\Aren\\Desktop\\DB\\REGISTRATION.mdf;integrated Security=true ; User Instance=True");
protected SqlCommand com = new SqlCommand();
protected SqlDataAdapter da = new SqlDataAdapter();
protected DataSet ds = new DataSet();
}
public DataSet GetStudent(string filter)
{
DataSet dset = new DataSet();
using (SqlCommand cmd = new SqlCommand())
{
string cmdstring = string.Format("Select * from {0}"
, Common.Data.Student.Table_Name);
if (!string.IsNullOrEmpty(filter)) cmdstring += " where " + filter;
cmd.CommandText = cmdstring;
cmd.Connection = this.con;
cmd.Connection.Open();
cmd.CommandText = cmdstring;
cmd.Connection = this.con;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dset, Common.Data.Student.Table_Name);
return dset;
}
}
NOTE: ALSO i got 3 project in my solution the DALBASE and GETSTUDENT methods are in one project and i call them from other project to get the data for me.
I believe i have encountered this problem in the past. in my situation I loaded an application in visual Studio which when run would load a database and attach to SQL Server. Somehow when I closed the application it didn't deattach the database from SQL SERVER and next time I ran the app it complained with a similar if not the same message.
If you have SSMS installed open up to the .\SQLEXPRESS instance and see if you have a Registration*.mdf file attached. Or if you don't have SSMS open up your command line and type...
sqlcmd -S .\SQLEXPRESS -Q "select name from sys.databases"
This will show you all your databases attached/online on that instance.
Hope this helps.
To query an excel sheet via SQL, I used to use either:
Dim excelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPath + ";Extended Properties=""Excel 8.0;IMEX=1;HDR=YES;"""
or
Dim excelConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + strPath + ";Extended Properties=""Excel 12.0;IMEX=1;HDR=YES;"""
Now this worked fine until I installed Office 2010.
Now I get a
Microsoft.Ace.OLEDB.12.0 provider is not registered on this machine
exception.
How can I find out the correct connection string/provider?
I believe for Excel 2010 it is :
Dim excelConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=D:\\MyDocs\\oledb.xlsx;Mode=Share Deny Write;Extended Properties=""HDR=YES;"";Jet OLEDB:Engine Type=37"
This appears to work in my visual studio, I got Excel to generate the query string and it had the extra entry in it.
I downloaded and installed Office System Driver: Data Connectivity Components as advised above - and the below code worked:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Password=\"\";User ID=Admin;Data Source=d:\\Sample.xlsx;Mode=Share Deny Write;Extended Properties=\"HDR=YES;\";Jet OLEDB:Engine Type=37";
OleDbConnection connection = new OleDbConnection(connectionString);
try
{
connection.Open();
OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet1$]", connection);
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = command;
DataSet ds = new DataSet();
adapter.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception)
{
//throw;
}
finally
{
connection.Close();
}
Perhaps you uninstalled the Access Database Engine (ACE) components? They are still available for download from MSDN as 2007 Office System Driver: Data Connectivity Components.