I am work on smartApplication, Here when am trying to connect to my SQL Server CE 2005 database, I get the exception
The path is not valid. Check the directory for the database. [ Path = D:\SmartProject\DBFile.sdf ]
My connection string is
Data Source=D:\SmartProject\DBFile.sdf;Password=test123
and the code to connect is like
string connectionString = "Data Source=D:\\SmartProject\\DBFile.sdf;Password=test123";
SqlCeConnection Connection = new SqlCeConnection(connectionString);
SqlCeCommand comm = new SqlCeCommand(SqlSelectCommandText, Connection);
SqlCeDataAdapter da = new SqlCeDataAdapter(comm);
DataSet ds = new DataSet();
try
{
Connection.Open();
da.Fill(ds);
if (ds.Tables.Count > 0)
dataTable = ds.Tables[0];
else
dataTable = new DataTable();
bsuccessfullyExecuted = true;
}
catch (SqlCeException ex)
{
bsuccessfullyExecuted = false;
dataTable = null;
}
finally
{
Connection.Close();
}
when the code try to open the connection it throw this exception provided the file is at the specified location or directory.
It works when I just place the DBFile.sdf file with the .exe in bin and remove the path except the Database file name from connectionstring.
but when I try to access it through Emulator it show this error. provided that its connect through cradle and Windows Mobile Device center.
It show all the page but when I try to access the Db it through exception..
Actually we have to put DBFile.sdf in Mobile Device Folder and now connection string would be
Data Source=\Temp\DBFile.sdf;Password=test123
this Temp is in the Mobile Device folder as our designer session connected to a SQL Mobile database which is on a mobile device connected through ActiveSync.
As a result, the connection string to the database which is automatically generated with the bindingsource is a special connection string that only works from within VS2005 and it begins with DataSource = Mobile Device.....
So for Emulator we have to put sdf file in Mobile Devide and as above
Related
Hy I am getting sqlException while trying to backup SQL Server database programatically in ASP.NET core in hosted application. Backup works properly on local maching but after deploying it is throwing SQL exception.
My code is
string fileName = Guid.NewGuid().ToString()+".bak";
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot","DatabaseBackups", fileName);
System.Console.WriteLine(filePath);
string connectionString = configuration.GetConnectionString("DefaultConnection");
var query = $"BACKUP DATABASE MySchool TO DISK='{filePath}'";
using ( SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
var command = new SqlCommand(query, conn);
command.ExecuteNonQuery();
Logger.LogInformation("BackUp Completed");
conn.Close();
}
If its permission issue on the server, I have given full permission to wwwwroot folder in server.
This question is answered many times in Stackoverflow, But I didn't get proper solution for my project.
Let me show you my code first:
namespace ConsoleDBManagement
{
class Program
{
static void Main(string[] args)
{
//Metioned here your database name
string dbname = "newDb";
SqlConnection sqlcon = new SqlConnection();
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
sqlcon.ConnectionString = #"Server=ABC-PC\SQLEXPRESS;database=" + dbname + ";uid=dran;pwd=sri;";
//Enter destination directory where backup file stored
string destdir = "D:\\Working Projects";
//Check that directory already there otherwise create
if (!System.IO.Directory.Exists(destdir))
{
System.IO.Directory.CreateDirectory("D:\\Working Projects");
}
try
{
//Open connection
sqlcon.Open();
//query to take backup database
//System.IO.File.Create("D:\\Working Projects\\FullBackUp.BAK");
sqlcmd = new SqlCommand("backup database newDb to disk='" + destdir + "\\FullBackUp.BAK'", sqlcon);
sqlcmd.ExecuteNonQuery();
//Close connection
sqlcon.Close();
//Response.Write("Backup database successfully");
}
catch (Exception ex)
{
//Response.Write("Error During backup database!");
}
}
}
}
I am getting exception while query executed.
Cannot open backup device 'D:\Working Projects\FullBackUp.BAK'. Operating system error 3 (The system cannot find the path specified.).
BACKUP DATABASE is terminating abnormally.
Please give me your suggestions.
When you run backup and/or other external file related commands when using a SQL auth login, the Windows security context is that of the SQL Service.
Your question is duplicate of Backup Permissions. Either grant permission for the SQL Service account/group, or run the backups using a Windows auth login that has permissions to the path.
I have the trouble with connection to MS SQL Server from Smart Device Project in Visual Studio 2008.
private void button2_Click(object sender, EventArgs e)
{
string connStr = "Data Source=SERVER-5;Initial Catalog=MydB;Integrated Security=SSPI;Connection Timeout=5";
DataTable data;
using (var connection = new SqlConnection(connStr))
{
try
{
var sda = new SqlDataAdapter("select * from pri_date", connection);
var ds = new DataSet();
sda.Fill(ds);
data = ds.Tables[0];
}
catch (SqlException ex)
{
var exc = ex.InnerException;
}
}
}
I'm trying to run this code in Visual Studio and I'm getting the error "Specified SQL server not found: SERVER-5" .
When I tried to launch this code in Windows application everything worked ok.
It's highly likely that it's a name resolution issue. If you use the server's IP address instead of the name, it will likely work. If that does work, then you need to make sure you have the DNS Server properly set for your device's network adapter.
I want to built a connection to a SQL Server database with a SELECT command.
The connection is ok but I get a error if I make a error. I want to get the Select values to a DataTable.
This I get if I try this:
The SELECT-Perssision was denied for UserApplicationRequests-Objekt, DB_CM0-Datenbank, dbo-Schema.
I use a Login Dialog in my application for building the connection string. In this form:
user id=[username];password=[password];server=[servername];Trusted_Connection=yes;database=DB_CM0
And here is my code for the SELECT command.
public DataTable GetDataTable(string sql)
{
using (con = new SqlConnection(connectionstring))
{
try
{
SqlCommand command = new SqlCommand(sql, con);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable tb = new DataTable();
adapter.Fill(tb);
con.Open();
command.ExecuteReader();
return tb;
}
catch (Exception)
{
return null;
}
}
}
My SQL command:
string sql = "SELECT * FROM [DB_CM0].[dbo].[UserApplicationRequests]";
its happening because of security issue..below steps might help you
Open SQL Server Management studio
Navigate to the database 'CNET_85731' >> Security >> Users
Right click on the one which you are using in your code
And finally, just uncheck 'db_denydatareader' inside "Database Role
membership" section.
Your connection string uses the sql authentication login method and integrated security login method simultaneously... Windows integrated security will have the priority in this case and attempt to use your windows user permissions to interact with the database... maybe this is not the behaviour you intended.
We have a existing closed source third party application using a Pervasive PSQL database. For Example the PSQL are located in the directory c:\test and have names like holiday.dat, offers.dat and so on. I want to read and if possible write to these files without having installed the Pervasive Workstation Engine. With the Workstation Engine and an ODBC connection it runs without any problems. But we won't install the Workstation Engine on any client and the third party application doesn't it, too.
On connectionsstrings.com i found the connection string:
"Provider=PervasiveOLEDB;Data Source=C:\datafilesDirectory;"
using directives:
using Pervasive.Data.SqlClient;
using System.Data.OleDb;
using System.Xml.Serialization;
test connection snippet:
string strAccessConn = "Provider=PervasiveOLEDB;Data Source=C:\datafilesDirectory;"
string strAccessSelect = "SELECT * FROM holidays";
// Create the dataset and add the Categories table to it:
DataSet myDataSet = new DataSet();
OleDbConnection myAccessConn = null;
try
{
myAccessConn = new OleDbConnection(strAccessConn);
}
catch(Exception ex)
{
Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
return;
}
try
{
OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect,myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
myAccessConn.Open();
myDataAdapter.Fill(myDataSet,"Categories");
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
return;
}
finally
{
myAccessConn.Close();
}
The application can't open the database connection.
In the connectionstring, you should replace C:\datafilesDirectory; with C:\test;.