Get data from remote sql server in a .NET web service [closed] - c#

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 created a simple web service in visual studio 2015 with C#. I have set up an sql server database with a table and i want to simply return any value from that table.
By searching i haven't found a very explanatory tutorial.
Should i add a server connection in VS and then use linq or use SqlConnection?
I also included an ADO Entity data model in the project but i don't know how to use it in the webMethod.
Does any good tutorial exist about all options that i have to connect to the db and provide some examples, too?

Since it's a web application I suggest saving the connection string into web.config.
Example:
<connectionStrings>
<add name="ConnStringNameHere" connectionString="Data Source=IPadress;Initial Catalog=databaseName;Integrated Security=False;User ID=name;Password=pass" providerName="System.Data.SqlClient" />
</connectionStrings>
You just have to add the user in SQL Server with the appropriate permissions.
That way you can have a couple of connection strings, each for a user type with specific permissions (some can only read, some can write, etc.)
You make a connection to the database with this simple code:
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connStringName"].ConnectionString))
using (SqlCommand cmd = new SqlCommand("SELECT * FROM user WHERE userName = username", connection))
{
connection.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
string id = (string)reader["Id"].ToString();
//work here
}
}
connection.Close();
}
And a useful link: http://www.codeproject.com/Articles/837599/Using-Csharp-to-connect-to-and-query-from-a-SQL-da

Related

Retrieve data from database in MYSQL without empty records [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 8 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
In MySql table have data and also have some empty values in the same row. Now I want to display that data into the user without that empty values. I'm not write any code yet
Welcome to Stack Overflow!
1.
I'm not too familiar with C# winforms, but this post can help you establish a connection to your MySQL database where you can query for specific data.
Code taken from that post:
string myConnectionString = "server=localhost;database=testDB;uid=root;pwd=abc123;";
private void button1_Click(object sender, EventArgs e)
{
MySqlConnection cnn = new MySqlConnection(myConnectionString);
try
{
cnn.Open();
MessageBox.Show ("Connection Open!");
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Cannot open connection!");
}
}
Data is retrieved from a MySQL database using a specific SQL (or "Structured Query Language"). In order to learn the basic MySQL syntax for queries, I would recommend a turorial like this.
3.
To send a SQL query to your database (via your established connection in Step 1) I would recommend looking at this post that gives an example on how to send a query via C# winforms
Code taken from that post:
public void Select(string filename)
{
string query = "SELECT * FROM banners WHERE file = '"+filename+"'";
//open connection
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);
//Execute command
cmd.ExecuteNonQuery();
//close connection
this.CloseConnection();
}
}
Overall, you're missing a few steps inbetween C# winforms and MySQL. Different languages USUALLY have an external library that facilitates: establishing a MySQL connection and launching MySQL queries. After obtaining the data from a MySQL query return, its up to you on how to display it to the user!
Hope this helps :D

do i have to install database on client's system? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am creating a C# Windows Forms application in Visual Studio 2019 for Car Dealing.
My question are:
Should I use local database as only single client on single PC is going to use the app?
Will I have to install database on client's PC?
If there is another way, how can I do it?
Sqlite would be a good solution to this.
https://www.sqlite.org/index.html
"SQLite is a C-language library that implements a small, fast, self-contained, high-reliability, full-featured, SQL database engine. SQLite is the most used database engine in the world. SQLite is built into all mobile phones and most computers and comes bundled inside countless other applications that people use every day."
As NeutralHandle mentioned, you can save the data into SQLite.
To use it in Winforms, you can follow the steps.
1.Install System.Data.SQLite from Nuget
2.Configure the connection string in App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
...
<connectionStrings>
<add name="SQLiteDbContext" connectionString="Data Source=MyDatabase.sqlite" providerName="System.Data.SQLite.EF6" />
</connectionStrings>
</configuration>
3.Then refer to the code demo.
SQLiteConnection.CreateFile("MyDatabase.sqlite");
SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite");
m_dbConnection.Open();
string sql = "create table highscores (name varchar(20), score int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "insert into highscores (name, score) values ('Me', 9001)";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();// read
SQLiteCommand sqlCom = new SQLiteCommand("Select * From highscores", m_dbConnection);
SQLiteDataReader sqlDataReader = sqlCom.ExecuteReader();
int i = 1;
while (sqlDataReader.Read())
{
listBox1.Items.Add(i);
listBox1.Items.Add(sqlDataReader.GetValue(0));
listBox1.Items.Add(sqlDataReader.GetValue(1));
i++;
}
m_dbConnection.Close();

How to access a SQL database in visual studio? (Razor cshtml, C#) [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 Already connected my SQL database with visual studio via server explorer And i filled some tables with values.
Now, I want to access/search through a table from my SQL database in my visual studio project.
For example: in my table i have products from different categories and i only want to display products from a specific category
But, how can in do that?
There is many ways by which you can access.If you are using connected mode Than you have to create object for connection string
SqlConnection con = new SqlConnection(#"Data Source=nameof_server;Initial Catalog=name_of_database;Integrated Security=True");
SqlCommand cmd = new SqlCommand("sql_command",con);
con.open()
//perform action based on your requirment
ExecuteNonQuery() // Use this for insert,update,delete
ExecuteScalar() // Use this for select single column
ExecuteReader() // Use this for select multiple records
con.close()
Please check this link

c# sql connection through internet [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 6 years ago.
Improve this question
I was wondering if can I connect to sql database from another computer without installing sql server (is this possible). I am creating a c# wpf program that would get and insert data in sql database that is placed in my PC, in all computers where this program is installed using internet. Should I use in connection string my internet IP, or what, because I’m confused?
My connection string
String connString = #"Network Library=dbmssocn;
Network Address=127.0.0.1,1433;
Integrated security=SSPI;
Initial Catalog=db";
Also i forgot to say that i'm new in programing area, and i want to connect to sql database through lan connection
Assuming that your remote database (on your other computer) accepts remote connections, you can accomplish this via a traditional ADO.NET connection or you could use an ORM like Entity Framework to handle the connection.
You can see an example below that takes advantage of the SqlConnection class which does exactly what it's name implies :
using(var sqlConnection = new SqlConnection("Your Connection String"))
{
var query = "Your Query";
using(var sqlCommand = new SqlCommand(query,sqlConnection))
{
sqlConnection.Open();
// Execute your query here using sqlCommand.ExecuteNonQuery()
// or some other execution method
}
}
You would just need to ensure that your actual connection string to target your remote database is correct.
Yes, it is possible. You could use ADO.NET or Entity Framework to accomplish this goal. However, I would recommend setting up a web api on the sever you have hosting the database, which you could call to get/post data. I personally wouldn't hard code any connection strings in the program you plan to deploy to end user workstations. Just call the web api and let your web api talk to the database.

Jira Integration in C# [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 7 years ago.
Improve this question
I am very new to this.So pardon me if I make any mistakes.
I am trying to read the Jira database.I just need to read it.
No write operations will be involved.
I am using C#.From what little I know, I think a connection has to be established with the Jira database using
SqlConnection conn=new SqlConnection(connectionstring);
And then I can read data using SqlReader.I have tried searching through the database and found few links like http://www.codeproject.com/Tips/762516/Connecting-to-Jira-using-Csharp
But I am not being able to understand.Can anyone help me out or direct me to few resources.
In the links that I searched through there are terms like "Rest API" etc. Do I need to know them ?
If you have access to the database, and want to read data that way, that's entirely possible using standard .NET objects, but you'll probably need to be decent at SQL to get the data out that you want.
Here's how you can (try to) access the database:
SqlDataReader rdr = null;
SqlConnection conn = new SqlConnection("YOUR_CONNECTION_STRING_HERE");
SqlCommand cmd = new SqlCommand("select * from whatever_jira_table", conn);
rdr = cmd.ExecuteReader();
//look up how to read from a reader
conn.Close();
conn.Dispose();
Another option is to use a Jira API which it looks like you can get from NuGet through Visual Studio. If you go this route, you need access to the Jira REST API, and it will expose a more friendly (different?) way to access data.
Either way, just go into Visual Studio, make a console app, and start adding code and stepping through with the debugger until thing start making sense.

Categories