i have problem with connection..
I can connect without problems to my database, when this database is downloaded on my computer:
viewdataConnection.ConnectionString = "User ID=sysdba;Password=masterkey;Database=localhost:F:\\machine.FDB;DataSource=localhost;Charset=NONE;"
everything is working fine.
But problems starts when i try to connect to this base in lan network:
viewdataConnection.ConnectionString = "User ID=sysdba;Password=masterkey;Database=10.48.14.51:d:\\backup\\machine.fdb; DataSource=:d:\\backup\\machine.fdb;Charset=NONE;";
and this don't work also:
viewdataConnection.ConnectionString = "User ID=sysdba;Password=masterkey;Database=10.48.14.51:d:\\backup\\maszyna.fdb; DataSource=:10.48.14.51:d:\\backup\\maszyna.fdb;Charset=NONE;";
then i get error about invalid token "second" in my sql query, but this query works when database is on localhost.. so probably there's mistake in my connectionstring..
Why?
So your datasource part should be datasource=10.48.14.51 without any extra path.
The database part should be your network path to the database like "database=\server\path\database.fdb"
Putting all together I will try in this way:
viewdataConnection.ConnectionString = "User ID=sysdba;Password=masterkey;" +
"Database=\\server\share\database.fdb;DataSource=10.48.14.51;Charset=NONE;";
of course you need to change \server\share\database.fdb with your exact network path.
Related
Momentarily i am working on an API. I just deployed my API to an online environment. now my problem i came across is the following:
Locally, my API had no problems whatsoever, the problem started when i deployed it.
I debugged my program and this was the problem:
var setups = _context.Setup;
foreach (var setup in setups) //<-- problem
{
if (setup.SetupID == setupItems.SetupID)
{
setup.SetupName = setupItems.SetupName;
counter++; //if it is already in the database
}
}
My new MSSQL database gets stuck on this foreach loop, it returns a 404 error the moment it gets in.
For comparison underneath are the developer as well as the deployment connection string:
Development
Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=***;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False; MultipleActiveResultSets=true
Deployment
Data Source=***;Initial Catalog=***;User ID=***;Password=***;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true
Question
How do I solve this issue?
UPDATE
The problem is that my online database items are not recognized.
I tried validating my connection string using this method in console:
$conn.ConnectionString = "myConncetionString" # whatever you are testing
$conn.Open()
$conn.Close()
The connection string connected perfectly.
I migrated all the tables in it as well that won't be the problem. for info I am using MSSQL MyLittleAdmin as database. Permissions are not the problem either.
Do you receiving any exception in your code? Please share exact exception you are receiving in your code.
You have two issues here - first: you should materialize the query (_context.Setup.ToList()) otherwise each row is materializing separately what generates a lot of communication between your app and the db. Secund: if your query (collection) will be materialized it is good idea to to stop tracking the changes in the foreach - better is to "search for the changes" just before "Save Changes" to the db
Here is the code, as I would implement it:
var setups = _context.Setup.Where(x => x.SetupID == setupItems.SetupID).ToList();
foreach (var setup in setups)
{
setup.SetupName = setupItems.SetupName;
}
I think I don't know exact context of your code - foreach through all records in the table is not very good idea :) even your exact issue is somewhere else..
I am trying to write data to a FILESTREAM column of an MS SQL database. When running it on the local machine, it works fine, but as soon as connecting to a remote one, it locks up.
Here is what I do (Using NHibernate):
private static readonly string QUERY_GET_PATH =
"select CompressedData.PathName() as path, GET_FILESTREAM_TRANSACTION_CONTEXT() as con from Data.TimeSeries where FS_ID = :dataId";
private static readonly string QUERY_SET_BLANK =
"update Data.TimeSeries set CompressedData = Cast('' as varbinary(max)) where FS_ID = :dataId";
// ...
ISession session = ...
var q1 = session.CreateSQLQuery(QUERY_SET_BLANK);
q1.SetGuid("dataId", ts.DataId);
q1.ExecuteUpdate();
var q2 = session.CreateSQLQuery(QUERY_GET_PATH);
q2.SetGuid("dataId", ts.DataId);
var results = q2.List();
var item = (object[])results[0];
var path = (string)item[0];
var context = (byte[])item[1];
return new SqlFileStream(path, context, FileAccess.Write);
Running this locally works fine, but then I connect to a remote database (this is not in my hands, I just have to write to it) using a connection string like this:
Server=10.0.0.5; Database=TheDatabase; User Id=foo; Password='barfoo';
The returned path looks like:
\\SOME-HOST\SOMEWHERE\v02-A60EC2F8-2B24-11DF-9CC3-AF2E56D89593\Foo\Data\TimeSeries\%!CompressedData\BAD566ED-CD86-42DE-AC71-D13125E89990\VolumeHint-HarddiskVolume1
First I thought it was because of name resolution from 10.0.0.5 to SOME-HOST but it keeps locking up when adding an appropriate entry to my hosts file (ping to the database works).
What might be wrong?
Followup
After waiting for some minutes, I get a Win32Exception with the message The user name or password is incorrect.
I am posting this as an answer so I can mark it as resolved although it is more like a workaround - but I'd like to at least leave an explanation here for others who might face the same or a similar issue.
As I found out, FILESTREAM only works with SSPI, and SSPI in turn only works when the client and server are either in the same domain or when accessing the database locally. This explains why it worked in first place when I accessed the database locally.
Since the database server is in a system set up by a third party working with us, I can't get my client machine into their domain, and I can't get their server into ours. Our workaround is to prepare everything, zip it up, transfer that archive to the database server and have a service running there, locally connected to the database, put everything in. From a security point of view that's okay because both systems only run in a local network with no access to the outside whatsoever.
I am trying to find out what drive a database is on, on the server. To further elaborate, there is a sever "MyServer" and three databases on that server. Databases A and B are on the "C:\" Drive of that server and database C is on the "D:\" Drive. I want to know how I can use the connection string to try and get the drive letter so that when a user selects a database from an application it tells them what drive that database is on.
Currently I am trying the following means and not getting the correct response:
Private void GetDriveLetter()
{
string connectionString = String.Format(DatabaseInfo.CONNECTIONSTRING_TEMPLATE, dbName, server)
try
{
using (SqlConnection cn = new SqlConnection(ConnectionString))
{
cn.Open();
Server srv = new Server(new ServerConnection(cn));
Database database = new Database(srv, dbName);
txtDriveLetter.Text = database.PrimaryFilePath;
cn.Close();
}
}
catch (Exception ex)
{
myException = ex.Message
}
}
DatabaseInfo is just a class we use for reading SQL Servers and database names, and myException is a function that writes exceptions to a log file. The issue I am having is that no matter what I do I get an exception that says PrimaryFilePath is never set. Am I doing something wrong/missing something, or is this just not the correct way to go about this?
EDIT: Sorry never posted the exception
To accomplish this action, set property PrimaryFilePath.
You could always run a query against your sys catalog views inside SQL Server:
SELECT * FROM sys.master_files
This will list your database, it's id, its logical name, and the location of the file on disk.
You should probably access the existing database, rather than constructing a new one:
Database database = srv.Databases[dbName];
(Of course, error handling required if the name doesn't exist).
Also, of course, you should be aware that a database may consist of multiple files, and those files may be on different drives.
If this is SQL Server, then you can use
SELECT * FROM sys.database_files
to get the files of the current database. So, your connection string should connect you to the database that you want and then the above SQL will give you lots of information that you might find useful, including the physical_name. From that you can extract the drive letter.
I'm using Visual Studio 2010 to build an ASP.NET web application, I'm working on dynamically populating (part) of the site map from information in a database. Right now I just have a dummy table in my App_Data folder, called DrugTest.mdf. The table is just called DrugTest1, which only has one field, DrugName. Where I'm hitting a wall is actually getting the data out of that table. Part of what I'm confused about is the connection string. I've looked at a lot of different information about connection strings, most notably http://www.connectionstrings.com/ but I'm a little confused as to how to actually apply said information to this project.
EDIT: I'm using SQL Server 2008 RC.
For example: Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
Password and User Id are pretty self-explanatory, but as far as I know I didn't get either one of those, I just added a table to the App_Data folder and filled it with dummy data. ServerAddress is a little confusing, because this information isn't really stored on a server, it's just stored locally. And I'm honestly not sure what Initial Catalog means.
Here's the code to populate the sub-tree. You'll notice the connection string is left blank.
string connString = ""; // get the connection string
string commandString = "SELECT drugName FROM DrugTable1";
SqlConnection connection = new SqlConnection(connString); // connect to db
SqlCommand command = new SqlCommand(commandString, connection); // set up the command
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet drugs = new DataSet();
adapter.Fill(drugs);
foreach (DataRow row in drugs.Tables[0].Rows)
{
string drugName = row["Name"] + "";
SiteMapNode node = new SiteMapNode(this, drugName,
"~/PlaceHolderUrl?path=" + drugName,
drugName);
AddNode(node, root);
}
Furthermore, I've got a nagging suspicion that I'm not going about this the right way. I think this will be the proper implementation once the database is up and running, but for right now I just want to get it working so it's ready to go - just slap in the proper connection string and table/field names.
So, finally, my question(s): How would I go about connecting to this local table? What format should my connection string be? I noticed there's a lot of them. Is there a better way to do this/am I doing this wrong?
Another way of getting the right connection string check this out in the ServerExplorer window
On the Menu click on View->Server Explorer
In the Server Explorer window locate DrugTest.mdf
Right click the file and select Properties
You can see the right connection string in the properties
Copy the connection string and use
Note: that the file location was hard-coded. You might need to use |DataDirectory| later
Try replacing the Initial Catalog portion of your connection string with AttachDbFilename=|DataDirectory|DrugTest.mdf.
Also, if you're using SQL Server Express, you might need to include the instance in the Data Source, so might try Data Source=mySeverAddress\SQLExpress, where SQLExpress is the instance name.
BTW, at the http://www.connectionstrings.com site, you can find this information in the SQL Server 2008 page if you scroll down a bit to the section titled "Attach a database file, located in the data directory, on connect to a local SQL Server Express instance."
I am using an ASP.Net and C# front end to run some reports. I want to open an SQL Connection to the data source used by the report.
When the report uses integrated security it is easy enough to create a connection, however I want to create a connection when the user name and password are stored by the reporting server.
I can get SQL Server path and initial catalogue, using
DataSource[] dataSources;
DataSourceReference dsReference;
DataSourceDefinition dsDefinition;
string dsPath;
ReportingService2005 rs = new ReportingService2005();
dataSources = rs.GetItemDataSources(reportPath);
if (dataSources.Length > 0)
{
dsReference = (DataSourceReference)dataSources[0].Item;
dsPath = dsReference.Reference;
dsDefinition = rs.GetDataSourceContents(dsPath);
// ....
}
I can also get the user name using
username = dsDefinition.UserName;
however if I use
password = dsDefinition.Password;
password = null, and can't be used to open the Sql Connection.
Is there a way to create an SQLConnection that uses the connection string and username and password credentials of a data source?
I am using Reporting Services 2008, and .NET 3.5 with web references to ReportService2005.asmx and ReportExecution2005.asmx
I really don't think it's possible, as this would pretty much constitute a security hole. I know this isn't the answer you are looking for, but I would parameterise the location of your reports (i.e. the IP/name of your MSRS server) and store them in your web.config along with a matching SQL instance. While it's not exactly what you're after, I think that's about as close as you are going to get.
I think you should read this page:
http://msdn.microsoft.com/en-us/library/reportservice2005.datasourcedefinition_properties.aspx
it shows the password field is write-only, so you can't set its value,but read it only.