Connect to SQL Server CE database with password - c#

How can I connect to the .sdf database with a password and still be able to use the tableadapter to FILL out my listbox with this code:
try
{
tbl_sClipManagerTableAdapter1.Fill(db_sClipManagerDataSet1.tbl_sClipManager);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
Produces this error:
The specified password does not match the database password. [ Data Source = PATH TO .SDF FILE ]
I have password protected the database. How can I connect with password and still through the tableadapter?
EDIT:
Oh, I don't want to store the password with the connection string - cause it is to easy readable through the config file.
SOLUTION:
I found the solution by myself.
In DataSet.Designer.cs find something like:
private void InitConnection() {
this._connection = new global::System.Data.SqlServerCe.SqlCeConnection();
this._connection.ConnectionString = global::sClipManager.Properties.Settings.Default.db_sClipManagerConnectionString + "; password=PASSWORDHERE";
I just added the + "; password=PASSWORDHERE".
Everything works now. And the password can't be easily read in the config file.
Best regards

I'm hoping that query is being executed by some kind of soap/rest service. If so, you can have that service run with domain credentials. Then you could give that domain-login access to the db. Then you could use integrated security in the connection string. That way the connection string has -zero- credential info.

Related

C# connect to remote server using Microsoft Terminal Services Active Client (RDP)

I have a piece of code that should connect to the server. The code is as following:
var rdp = new MsRdpClient8NotSafeForScripting();
rdp.Server = "192.168.0.101"; //adress
rdp.Domain = "localdomain"; //domain
rdp.UserName = "test"; //login
rdp.AdvancedSettings8.ClearTextPassword = "123456";//password
try
{
rdp.Connect();
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine(rdp.Connected);
if (rdp.Connected != 0)
{
rdp.Disconnect();
}
Console.ReadLine();
This is supposed to "connect" to my remote server via 3389 port so that I can be able to read a file from my desktop which is called: "min.txt"
So far I have tried specifying the login data of my server but I always just get output of "0" in the console's window, regardless of whether I specify correct or incorrect login data..
My questions here are:
Why is it connecting even with wrong login data (ip, user + password)
How can I, once I've been indeed successfully connected to the server, access the min.txt file on my remote server, which is located at desktop...
Can someone help me out?
Probably you can try specifying the password like below:
MSTSClib.IMsTscNonScriptable secured = (MSTSClib.IMsTscNonScriptable)rdp.GetOcx();
secured.ClearTextPassword = “123456”;
For reference: MSDN link is here
Once connected, you can access the file like a shared network file via UNC.
Example:
System.IO.FileStream stream = System.IO.File.OpenRead("\\servername\sharedname\path\somefile.txt");
Then need to ensure that permissions are in place to access the folder.

ASP.NET database connection closes when launching website

I have decided to start programming in ASP.NET MVC using C#
I have been following tutorials and implementing them into my own project.
What happens is that once i click Debug in Chrome/Firefox etc my database connection closes (the red cross appears)! (But when I refresh it manually it has the tick - till that point) I have a feeling it's to do with the connectionString in web.config. I don't like connection strings, to me they seem full of jargon. How many parameters do you REALLY need for a test project?
<connectionStrings>
<add name="PetsDBEntities" connectionString="metadata=res://*/MainDBModel.csdl|res://*/MainDBModel.ssdl|res://*/MainDBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|E:\Visual Studio 2013\Projects\Pets\Pets\App_Data\PetsDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Unfortunately my knowledge of c#/asp.net mvc is VERY limited, hence following a tutorial but I know you guys at SO are really good at what you do.
The database named PetsDBEntities has the following properties.
Data Source: Microsoft SQL Server Database File (SQL Client)
Database File Name: projects/app_data/PetsDB.mdf
If i right click the database and click >Modify Connection and then click test connection I get a "This connection cannot be tested because the specified database file does not exist"
This is where the code breaks. NOTE: The values being submitted are passed through, it just seems that it is unable to connect and add the row to the database.
if (ModelState.IsValid)
{
using( var db = new PetsDBEntities())
{
var crypto = new SimpleCrypto.PBKDF2();
var encrpPass = crypto.Compute(user.Password);
var regUser = db.Users.Create();
regUser.Email = user.Email;
regUser.Password = encrpPass;
regUser.PasswordSalt = crypto.Salt;
regUser.UserID = Guid.NewGuid();
db.Users.Add(regUser);
db.SaveChanges(); //--------THIS IS WHERE I GET AN ERROR---------
return RedirectToAction("Index", "Pictures");
}
}
else
{
ModelState.AddModelError("", "Login Data is incorrect!");
}
return View();
Hopefully this is enough information for you guys to spot where the problem is.
Thansk for anyone that can help! Again, I am new and not entirely sure what information is completely needed.
Your issue is likely with the db filename.
attachdbfilename=|DataDirectory|E:\Visual Studio 2013\Projects\Pets\Pets\App_Data\PetsDB.mdf;
Should be:
attachdbfilename=|DataDirectory|\PetsDB.mdf;
Maybe this can help?
A simple connection string for SQL Server could be Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;
You mentioned that when you click on test connection, you are getting an error. Your question title might be misleading, as i can assume that you didn't established the connection at all.
Can you go to "Server Explorer" -> "Data Connections" -> "Add Connection" -> "Microsoft SQL Server Database File", i assume you are using the .mdf file directly instead of connecting to SQL server?
Click on "Test Connection" and if everything goes well, you can retrieve the connectionstring from the properties.

MySql access denied for user - can't create the user the software is looking for

My connection string looks like this:
string server = "localhost";
string database = "stman";
string user = "logan";
string pass = "root";
string connStr = String.Format("Data Source={0};Initial Catalog={1}; User Id={2}; Password={3};", server, database, user, pass);
Given my understanding, this means MySql should be using 'logan'#'localhost' for the login, but it isn't.
It looks as though it's using 'logan'#<server's fully qualified name>:
Access denied for user 'logan'#'HP-PL-ML110.young.home'
I'm at a loss right now. SqlYog doesn't allow me to create a user with that hostname, my options for hostname are %, localhost or 127.0.0.1
Can anyone help me make sure that the website is using the specified username here? I have no idea what to look at to fix this
Joe's comment:
Have you tried disabling host name lookup, as in this question
Applying this didn't fix the issue but it did change the settings so that I could use the IP address in the connection string.
This does mean I had to create a new user (I found that, in Sqlyog, you can enter something different in the hostname field in user creation) and grant the new user the required permissions on the target database.
It's not exactly a solution, but it is a viable workaround that's got me back to a point where I can actually progress further in my development now.
Thanks Joe
string connStr = String.Format("Data Source={0};Initial Catalog={1}; User Id={2}; Password={4};", server, database, user, pass);
Should be
string connStr = String.Format("Server={0};Database={1};Uid={2};Pwd={4};", server, database, user, pass);

SQLConnection connection error "Login failed for user server/guest"

I keep receiving a connection error. I know that the information is right, as I tried them out with SSMS now several times in the last few minutes, so the problem is in the C# connection string. According to various Google searches, the syntax is right, just it does not work.
try
{
// Get the connection information.
GetSQLConnectInfo(ref sConnect);
// Formulate the connection string.
String strConnect = String.Format("Server=myserver;Database=mydb;User Id=myusername;Password=mypassword;Trusted_Connection=yes;connection timeout=30");
// DATABASE: Create the connection.
SqlConnection oConnection = new SqlConnection(strConnect);
oConnection.Open();
if (ConnectionState.Open != oConnection.State)
return false;
return true;
}
catch
{
}
Error comes back with: Login failed for user 'myserver\Guest'.
Error Code: -2146232060
Error Number: 18456
It would seem that judging by the error message, the Open method does not recognize the user name, as Open tries to use a guest account, which obviusly will not work.
Am I missing something? I am using SQL Server authentication.
Remove Trusted_Connection=yes. That will override your SQL Authentication (username/password) settings and try to log in as the user running the app.

How do I get the Application Name from either SQL Server connection string or the IIS application name using C#?

How do I get the Application Name from an SQL Server connection string in the web.config file.
I want to use to log escalated error messages from a web application into the Windows Event Log.
Maybe there is a better way of doing this, ie using the IIS/Web application name?
Thanks
Mark
What does the connection string look like?
DbConnectionStringBuilder is good for parsing and inspecting connection-string values by key:
DbConnectionStringBuilder db = new DbConnectionStringBuilder();
db.ConnectionString = connectionString;
Console.WriteLine(db["Application Name"]);
otherwise you can get various details from the http server variables.
SqlConnectionStringBuilder is also useful if you are using SQL Server:
SqlConnectionStringBuilder sc = new SqlConnectionStringBuilder(connectionString);
string applicationName = sc.ApplicationName;

Categories