I have some problems with connecting to my Microsoft SQL Server 2012. Here is my code:
private void conButton_Click(object sender, EventArgs e)
{
string conString;
SqlConnection appMessage;
conString = #"Data Source=DHRANTONIUSVICT\SQLA; Initial Catalog=AppMessage; User ID=myid; Password=mypass; Server=localhost\sqlexpress";
appMessage = new SqlConnection(conString);
try
{
appMessage.Open();
MessageBox.Show("Connection is stable. Starting up the engines...");
}
catch (Exception)
{
MessageBox.Show("Connection to HQ is unstable. Need engineers ASAP!");
}
Somehow I can't connect to my SQL Server.
What am I doing wrong?
And please, bare in mind that I`m new to this SQL stuff.
Use DataSource or Server.You are using both
conString = #"Data Source=DHRANTONIUSVICT\SQLA; Initial Catalog=AppMessage; User ID=myid; Password=mypass ";
please refer to the SqlConnection.ConnectionString documentation:
Related
I'm very new to coding, and recently got a task to connect my SSMS/SWL Database to Visual Studio 2019.
When I press the button, the DB should connect and a message should pop up saying it's been connected.
I assume I have the incorrect login details, but I'm not sure, and even if I was sure, I wouldn't know how to find the correct login details.
So here's my current button click code:
private void button1_Click(object sender, EventArgs e)
{
string connectionString;
SqlConnection cnn;
connectionString = #"Data Source=TRISTAN\SQLEXPRESS ;Initial Catalog=TutorialDB ;User ID=TRISTAN\Tristan; Password=";
cnn = new SqlConnection(connectionString);
cnn.Open();
MessageBox.Show("Connection Open");
cnn.Close();
}
So, the login detail's I'm using are those shown when launching SSMS and the "Connect to Server" box pops up.
Am i using the login details from the correct place "Connect to server window" Or are the details I need somewhere else.
The error message I get when clicking the 'Connect' button "System.Data.SqlClient.SqlException: 'Login failed for user 'TRISTAN\Tristan'.'"
I'd appreciate any assistance, please also keep in mind I don't know much Programming language yet, so please keep it simple.
Open Visual Studio
Go to View tab -> Server Explorer
Right click to "Data Connections" tab
Add connection
Select the server name
Select the database
Ok
Server Explorer -> Data Connections -> myServer\myDB.dbo -> right click and then Properties
Copy the connection string from Properties View
Write this code;
string connectionString;
SqlConnection cnn;
connectionString = #"***YOUR_CONNECTION_STRING***";
cnn = new SqlConnection(connectionString);
cnn.Open();
if (cnn.State == System.Data.ConnectionState.Open)
{
MessageBox.Show("Connection Open");
cnn.Close();
}
if (cnn.State == System.Data.ConnectionState.Closed)
{
MessageBox.Show("Connection Closed");
}
You should catch error while opening a connection and display it to the user, something like this:
private void button1_Click(object sender, EventArgs e)
{
string connectionString = #"Data Source=TRISTAN\SQLEXPRESS;Initial Catalog=TutorialDB;Trusted_Connection=True;";
using (var cnn = new SqlConnection(connectionString))
{
try
{
cnn.Open();
MessageBox.Show("Connection Open");
}
catch (SqlException ex)
{
MessageBox.Show("Error while connecting to database: " + ex.Message);
}
finally
{
cnn.Close();
}
}
}
I'm new to c# and im trying to make a store bill generator for fun and learning. Since I don't know much about databases Access seemed decently simple.
I'm just trying to give user confirmation that the connection to the db is successful, I've connected the db to my project but when I run the program nothing happens
I've tried giving the status in a label but it doesn't show either.
availableproductsbox is a listbox
private void availableproductsbox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\admin\source\repos\MultiCashTrack\MultiCashTrack\items.accdb;
Persist Security Info=False;";
connection.Open();
MessageBox.Show("Database connected");
connection.Close();
}
catch(Exception ex)
{
MessageBox.Show("Database connection failed"+ ex);
}
}
I expect it to show a message box showing "Database connected" but it doesn't show
I have a C# Windows app that I am creating with connection to a MS SQL server. I need the ability to switch to a local instance of SQL Server express if connectivity is down. This app is in a remote area where online is hit and miss. When connectivity goes down, I need the connection strings to reflect to the local server. I have logic to resync items from the local db to the live one once connection is restored. However, I am having issues trying to test connectivity automatically. The connection string in my app.config has both live and offline connections with the following code:
SqlConnection con = new SqlConnection(Properties.Settings.Default.MyLiveConnectionString);
If connection goes down, I want the local connection to use:
SqlConnection con = new SqlConnection(Properties.Settings.Default.MyLocalConnectionString);
Does anyone have a suggestion on how to make this happen seamlessly and quick?
I can test connections using try/catch, but it takes several seconds to time out before continuing.
public void Connect()
{
string connectionStr = string.Empty;
try
{
if(CheckConnection(Properties.Settings.Default.MyLiveConnectionString))
connectionStr = Properties.Settings.Default.MyLiveConnectionString;
}
catch (Exception ex)
{
//Log this, etc...
}
if (string.IsNullOrWhiteSpace(connectionStr))
connectionStr = Properties.Settings.Default.MyLocalConnectionString;
using (SqlConnection conn = new SqlConnection(connectionStr))
{
}
}
private static bool CheckConnection(string connectionString)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("SELECT 1", conn);
conn.Open();
if ((int)cmd.ExecuteScalar() == 1)
return true;
}
return false;
}
I'm using the following to retrieve data from a database but the SqlConnection won't open. It throws an error at scon.Open(). I'm sure it's elementary but I can't work it out.
protected void Page_Load(object sender, EventArgs e) {
SqlConnection scon = new SqlConnection("Data Source = .\\SQLEXPRESS; Database = populate.mdf; Integrated Security = true; Initial Catalog = populate");
StringBuilder htmlString = new StringBuilder();
if(!IsPostBack)
{
using (SqlCommand scmd = new SqlCommand())
{
scmd.Connection = scon;
scmd.CommandType = CommandType.Text;
scmd.CommandText = "SELECT * FROM populate";
scon.Open();
SqlDataReader articleReader = scmd.ExecuteReader();
htmlString.Append("'Populate page:'");
if (articleReader.HasRows)
{
while (articleReader.Read())
{
htmlString.Append(articleReader["dateTime"]);
htmlString.Append(articleReader["firstName"]);
htmlString.Append(articleReader["lastName"]);
htmlString.Append(articleReader["address"]);
htmlString.Append(articleReader["details"]);
}
populatePlaceHolder.Controls.Add(new Literal { Text = htmlString.ToString() });
articleReader.Close();
articleReader.Dispose();
}
}
}
}
I'm using this link https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx as one of my references. I'm also using SQL Server 2008 R2 Express if these information are of any help.
Here's part of the error message:
SqlException (0x80131904): Cannot open database "populate" requested
by the login. The login failed.
Any help would be greatly appreciated.
Quoted from https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx#sse, if you want to use .mdf file as a database, you should use the following connection string containing AttacheDbFileName.
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" />
I solved it. All the connection string and other code was correct. The database just needed connecting to the Management Studio with some extra attention.
I am trying to connect to database in my server which is hosted on eris.ignitionserver.net:2083/. it is showing me error that network related error occured. i am posting my code here .. so Please provide me some help
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=eris.ignitionserver.net:2083; Initial Catalog=TISDB; User id=abc; Password=12345;Integrated Security=SSPI;";
conn.Open();
}