I am using C# official MongoDB nugget library.
I hosted the web service on Windows Azure, and the MongoDB database at Mongolab.
I connected to a server with the code below:
connection = mongodb://user:pass#ds049999.mongolab.com:45077"
_server = MongoServer.Create(connection);
Previously it is working fine and it work fine at local testing server, but at my latest publish I get error below:
ExceptionMessage":"Invalid keyword 'data source'."," ....... at MongoDB.Driver.MongoConnectionStringBuilder.set_Item(String keyword, Object value)
Anyone know what is the problem?
The following works for me:
var client = new MongoClient("mongodb://user:pass#ds049999.mongolab.com:45077");
var server = client.GetServer();
var database = server.GetDatabase("MyDataBaseName");
If your username or password contains special characters, you might want to encode them.
Please also note that your database username and password may well be different to your MongoLab login!
Related
I'm trying connect to mySQL server with code below: (just part of my whole code)
using MySql.Data.MySqlClient;
//...
public void ConnectToServer()
{
string ConnectionString =
"Server=DESKTOP-91JG566;Database=db_server;Uid=user;Pwd=123456A+;";
MySqlConnection cConn = new MySqlConnection(ConnectionString);
cConn.Open(); // this returns the exception
serverStatus = cConn.Ping() ? serverStatus = "connected" : serverStatus = "disconnected";
}
I'm using MySQL Workbench, there is my server with database https://i.stack.imgur.com/jxn84.jpg
The exception says: System.TypeInitializationException: 'The type initializer for 'MySql.Data.MySqlClient.MySqlConfiguration' threw an exception.'
I have searched about it, and even though I enabled "SQL Server Debugging" in project properties, things are the same.
The problem might be caused because I have bad connection string, I am not sure.
My goal is to communicate with the server, query him, reveive orders etc...
As Jason already mentioned this is a horrible idea to do. Instead, I suggest you build some mini REST API or even use some of the serverless services out there.
When you have that REST API or Azure Function ready then you can communicate with them using your C# code, more precisely using HttpClient from the .NET.
Your mobile app will be the client which will "talk" to REST API, and REST API will proceed with your request and make the request to the DB, grab some data and return you JSON or XML which you can, later on, deserialize into C# objects and show to the user.
The most simple example is located on the MS Docs page here, so you can take a look.
Wishing you lots of luck with coding!
I've been trying to get some initial code working before I start working on my app.
I have had this working maybe a year ago so something tells me there is an update/version issue.
But any help is good so.
I have a raspberry pi set up with apache, MariaDB, php etc etc. I have a simple webpage running so apache is fine, I have another webpage that pulls data from a table so MariaDB is fine (fine-ish, given my issue). I have myphpadmin set up and can log in create new db's and users etc.
Now I would like to have a UWP app interact with a db hosted on my pi. I created a new user with SELECT and INSERT only for a specific db (hopefully this reduces security issues but I'm new so maybe I'm wishing).
I also then found the mariadb config file and commented the bind-address line. The skip-networking line is no longer in the file so just ignored that. (this is what the docs say to allow remote connections).
I have opened port 3306 on my router, and I also have a domain name and use no-ip but I assume this is all fine as apache runs fine. Saw someone talking about SSH, I changed my ssh port but I don't think that would be the issue.
Then the UWP.
Last time I tried this I had to use MySQL Connector Net 6.7.9 as newer versions didn't work with RT. So I have added this to the project references.
Now for simple testing the connection I have a button and TextBlock, click the button to attempt connecting and output the exception/result.
Have tried conn string builder as well as just a straight forward string, neither work.
private void ConnectDatabase_Click(object sender, RoutedEventArgs e)
{
string ConnString;
MySqlConnection Conn;
MySqlConnectionStringBuilder Csb = new MySqlConnectionStringBuilder();
Csb.Server = "http://rnd-domain.me";
Csb.Port = 3306;
Csb.UserID = "usr";
Csb.Password = "passwd";
Csb.Database = "testdb";
ConnString = "server=http://rnd-domain.me;database=testdb;uid=usr;pwd=passwd;";
Conn = new MySqlConnection(ConnString);
try
{
Conn.Open();
DbUpdateText.Text = "connected";
Conn.Close();
}
catch(Exception ex)
{
DbUpdateText.Text = ex.Message;
}
}
When I click the button the ex.Message is 'Unable to connect to any of the specified MySQL hosts.'.
There are no errors thrown with the app, just this unhelpful message.
I've googled this and wasted 80% of my sunday trying to get this working. I've added sslmode=none, charset=utf8, port=3306. I tried using server=localhost or using my current ip and even using my mariadb master user (not root).
What could be the issue?
Thanks.
Edit:
I just used the pi's internal IP and got a new ex.Message, understood this one about SSl, added sslmode=none and all is working.
So now its just getting around the domain name issue?
Your connection string isn't quite right.
You probably want this:
server=rnd-domain.me;database=testdb;uid=usr;pwd=REDACTED;
It doesn't make sense to mention http:// in a MySQL connection string, because the connection doesn't use the HTTP protocol. Rather it uses the MySQL protocol.
The issue was using a pc (client) and raspi (server) both inside the home network.
When I changed the host to the internal IP of the raspi it all worked fine.
Driver Language - C#
Driver Version - 2.7.0
DB Version - 4.0.0
.NET Framework 4.6.1
Exception message (credentials and server removed)
The connection string
'mongodb+srv://USER:PASS#uat-xxxx.mongodb.net/test?retryWrites=true'
is not valid.
Code
var client = new MongoClient(#"mongodb+srv://USER:PASS#uat-xxxx.mongodb.net/test?retryWrites=true");
User and password contain no special characters
No connection attempt is made to the server.
However, this works fine if i use the URI version
var client = new MongoClient(#"mongodb://USER:PASS#uat-shard-00-00-xxxxx.mongodb.net:27017,uat-shard-00-01-xxxxx.mongodb.net:27017,uat-shard-00-02-xxxxx.mongodb.net:27017/test?ssl=true&replicaSet=UAT-shard-0&authSource=admin&retryWrites=true");
I've tried to decipher the validation rules from the source but it's beyond my regex ability.
The srv connection string looks fine to me and is exactly the same as that presented to me in the Mongo Atlas UI.
Any idea what i'm doing wrong?
Thanks
I attempted to reproduce this using
var client = new MongoClient("mongodb+srv://USER:PASS#cluster0-xxxx.mongodb.net/test?retryWrites=true");
var dbs = await client.ListDatabaseNames().ToListAsync();
Console.WriteLine(dbs);
Which gave me the expected output (2 database names).
My test environment is .NET 4.6.1, LINQPad, and C# Driver 2.7.0.
You can try eliminating the C# Driver (and framework) from the equation by using the Mongo Shell to test. If you're using Mongo Shell version 3.6 or later, you can test the connection with a command similar to
mongo "mongodb+srv://cluster0-xxxx.mongodb.net/test" --username USER
That being said, you should be able to contact MongoDB Support for your atlas cluster, using the "Support" link on the left side of the Atlas UI. They should be able to help.
I am using MongoDb server installed on VM Ubuntu 14 on Azure, and I use this
tutroial, with last version. I add the port of mongo 27017 too.
And I connect to it directly and add Database with some collections.
I use the mongoDb .Net Driver on VS2015 in C# with version 2.0.1 (using link)
and try to connect to the Mongo Server, but the state of the server is disconnected
var client = new MongoClient("mongodb://name.cloudapp.net:27017");
var state = client.Cluster.Description.State;
MessageBox.Show(state.ToString());
I used it before the same steps and nothing happen, just I don't know where is the problem
Try this please. I think this may work. Just have to add one line to enumerate all databases.
var client = new MongoClient("mongodb://name.cloudapp.net:27017");
var databases = client.ListDatabases();
var state = client.Cluster.Description.State;
MessageBox.Show(state.ToString());
This answer explains better.
I get the following error message, when I try to connect to SQL Azure.
Windows logins are not supported in this version of SQL Server
I'm using an Azure connection string. On development I'm running against SQL Server Express. This specific error is thrown when I try to fetch some data from the database.
The context that I'm using is running in a using clause, see below
function List<SomeType> GetList(string dbContextName)
{
using (MyDbContext context = new MyDbContext)
{
return context.SomeTypes.ToList();
}
}
We're using Entity Framework version 4.2, ASP.NET MVC 3 and .NET 4.0.
How can I resolve this issue?
I was using user/pass and still got the error message. But, I added this to my connection string and it worked.
Trusted_Connection=False;Encrypt=True;
Set
Integrated Security=False
In Connection String.
You've probably used the incorrect connection string, this is the connection string format that worked for my case:
"ConnectionString": "Server=tcp:xxxx.database.windows.net,1433;Database=xxx;User ID=xxx;Password=xxx;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
Integrated authentication (i.e. SSPI in the connection string) is NOT supported in SQL Azure.
Only SQL Authentication is supported (i.e. username & password in the connection string)
As already mentioned by others, only SQL Server authentication is supported in SQL Azure.
You can read more on Guidelines and Limitations with SQL Azure. As well as the Security Guidelines and limitations for SQL Azure.
You have to CREATE LOGIN by yourself in your MASTER database, then you will want to CREATE USER in your custom Azure Database. Also do not forget to execute sys.sp_addrolemember to grant some permissions to your user.
More on managing users and logins in SQL Azure can be found here.
And, at the end, you can always look at the invaluable source for connection strings.
1.**Windows Authentication** is not supported in Azure so you should go with **SQL Server Authentication**.
2.When you use SQL server Authentication you should pass User Id(Login in SQL server) and Password(Password in SQL server).
3.User Id should contain space in between should not be like UserId.
4.Trusted_Connection should be false.
The connection string in *appsettings.json* look like this:
"ConnectionStrings": {
"DBContext": "Server=ServerName;Database=DbName;User Id=loginName;Password=loginPassword;Trusted_Connection=false;MultipleActiveResultSets=true"
}