what is the query of sql database connection [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to connect my C# project with SQL database.
which query I have to follow??
using(SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
}
i use this, it is not working

Try this
namespace Town
{
class clsconnection
{
string connection = #"Data Source=server name;Initial Catalog=databasename;Integrated Security=True";
public SqlConnection getconnection()
{
SqlConnection sql = new SqlConnection(connection);
sql.Open();
return sql;
}
}
}

Related

Connecting REST WebService to MySQL database [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to create a REST WebService which is connected to a MySQL database. But I got stuck at a point and don´t really know how to get further.
This is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SimpleRESTServer.Models;
using MySql.Data;
namespace SimpleRESTServer
{
public class PersonPersistance
{
private MySql.Data.MySqlClient.MySqlConnection conn;
public PersonPersistance()
{
string myConnectionString;
myConnectionString = "server=127.0.0.1;uid=Local Instance MySQL80; pwd=;database=employeedb";
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = myConnectionString;
conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
}
}
public long savePerson(Person personToSave)
{
String sqlString = " INSERT INTO tblpersonnel (FirstName, LastName) VALUES ('"+ personToSave.FirstName + "','" + personToSave.LastName + "')";}
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(sqlString, conn);
cmd.ExecuteNonQuery();
long id = cmd.LastInsertedId;
return id;
}
}
So my problem is that in the savePerson function something doesn´t work. When I write 'sqlString' and 'conn' ind the MySqlCommand part, it always get underlined red. I don´t really know what´s wrong.
I listend to this guys tutorial: https://youtu.be/LpySuvYPMZQ
I hope somebody can help me.
Thanks for your help in advance!
You are closing the method at the sqlString variable. Just after the semi colon you have a closing curly braces.

How to access Sql server online database with my C# windows application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have windows application using C#, i want to store the data in local SQL SERVER database and as well as online SQL SERVER Database.
You shouldn't be asking questions like this here, but here's something to get you started.
using(SqlConnection cnn = new SqlConnection())
{
cnn.ConnectionString="your connection string";
// /*e.g.: */
cnn.ConnectionString = "Server=123.123.123.123,1433;Database=DbName;User id=user;Password=pass;";
using(SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.StoredProcedure; //or whatever it is
cmd.ConnectionTimeout = 0; //or however long you want
cmd.Connection = cnn;
cmd.CommandText = "your query";
//cmd.Parameters.AddWithValue("#SP_PARAM", object); if you are calling an SP
cnn.Open();
cmd.ExecuteNonQuery(); //whatever you are executing
cnn.Close();
}
}

c# "ExecuteNonQuery requires an open and available Connection. The connection's current state is closed." [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
hello i have a problem with getting exception of "ExecuteNonQuery requires an open and available Connection. The connection's current state is closed."
public void AddCustomer(Customer customer)
{
string connectionString = #"Data Source=LIRON-PC\SQLEXPRESS;Initial Catalog=C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\MatokMmagnet.mdf;Integrated Security=True";
using (m_sqlConnection = new SqlConnection(connectionString))
{
m_cmd = new SqlCommand();
m_cmd.CommandType = CommandType.Text;
m_cmd.Connection = m_sqlConnection;
m_cmd.Parameters.AddWithValue("#id", customer.id);
m_cmd.Parameters.AddWithValue("#FirstName", customer.FirstName);
m_cmd.Parameters.AddWithValue("#LastName", customer.LastName);
m_cmd.Parameters.AddWithValue("#Password", customer.Password);
m_cmd.CommandText = "INSERT INTO Customers (id, FirstName, LastName, Password)VALUES (#id, #FirstName, #LastName, #Password)";
try
{
m_cmd.ExecuteNonQuery();
}
catch(Exception e)
{
Console.WriteLine( e.Message);
}
finally
{
m_sqlConnection.Close();
}
}
}
Like the error says you must open the connection before executing the query like this :
public void AddCustomer(Customer customer)
{
string connectionString = #"Data Source=LIRON-PC\SQLEXPRESS;Initial Catalog=C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\MatokMmagnet.mdf;Integrated Security=True";
using (m_sqlConnection = new SqlConnection(connectionString))
{`
m_sqlConnection.open();
//....
}
}

ExecuteReader: Connection property has not been initialized. Browser Game [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
We are having some problems displaying the experience a player has from our database.
This is for a school project, would like some hints :)
SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
SqlCommand CheckExp = new SqlCommand("SELECT Experience FROM Player WHERE UserID=#uid");
string uID = Session["userID"].ToString();
CheckExp.Parameters.AddWithValue("#uid", uID);
try
{
connection.Open();
SqlDataReader ExpReader = null;
ExpReader = CheckExp.ExecuteReader();
if (ExpReader.Read())
{
Label6.Text = ExpReader["Experience"].ToString();
}
}
catch (Exception ex)
{
Label6.Text=(ex.Message);
}
finally
{
connection.Close();
}
You didn't connect your SqlConnection and SqlCommand.
Just define your connection as a second parameter like;
SqlCommand CheckExp = new SqlCommand("SELECT Experience FROM Player WHERE UserID=#uid", connection);
Or you can assing your SqlCommand.Connection property like;
CheckExp.Connection = connection;

Insert record into Access 2000 database in C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I need to insert a record in Access 2000 database using C#. The code fails at the SqlConnection. Please help.
string connectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Trading.mdb";
string commandText = "INSERT INTO Order (OpenDate) VALUES (#OpenDate)";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.AddWithValue("#OpenDate", DateTime.Now);
try
{
command.Connection.Open();
int response = command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Error: {0}" + ex.Message);
}
}
Problem : you are working with MS-Access Database but using SqlServer objects.
Solution : you need to use OleDbConnection object instead of SqlConnection and OleDbCommand instead of SqlCommand
Try This:
string connectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Trading.mdb";
string commandText = "INSERT INTO Order (OpenDate) VALUES (?)";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(commandText, connection);
command.Parameters.AddWithValue("#OpenDate", DateTime.Now);
try
{
command.Connection.Open();
int response = command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Error: {0}" + ex.Message);
}
}
You need OleDbConnection not SqlConnection which is used for SQL Server.
See: Walkthrough: Editing an Access Database with ADO.NET
also: How to: Create Connections to Access Databases

Categories

Resources