How to call code from SQL in C# [closed] - c#

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
I have two classes in which the teachers have merged their final projects into one, one class is software engineering and another one is data bases. The thing is that for SE i have to develop a desktop/smartphone app and for DB i have to develop every DB related stuff for that app.
But i have to keep both things separated, i mean i have to keep C# code away from SQL code so i can't do queries or any stuff using selection strings and such, i just have to call stored procedures with said queries from code.
Any idea how could i do that? To summarize i just want to call any code or procedure that i write in sql and store it's values in a variable,object or array.
As i said i cannot use:
string selectstr = "SELECT * FROM students;"
and execute that query, i have to write that in sql and call it from C# and store the values returned.

Stored procedures are called like any other SQL command in C#:
using (SqlCommand cmd = new SqlCommand("MyStoredProcedure", connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#myParameter1", value);
...
using (SqlDataReader reader = cmd.ExecuteReader())
{
...
}
}
The "magic" bit is to set the command type correctly ;-)

Related

Stored procedure vs simple SQL performance comparison [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 3 years ago.
Improve this question
Performance comparison between a stored procedures and simple SQL queries in C# OR linq queries.
CREATE PROCEDURE [dbo].[GetProductCM]
AS
BEGIN
SET NOCOUNT ON
SELECT
p.ProductId,
p.ProductName,
p.CategoryId
FROM
dbo.Product p
END
using (SqlConnection myConnection = new SqlConnection(con))
{
string oString = "SELECT p.ProductId, p.ProductName, p.CategoryI FROM dbo.Product p";
SqlCommand oCmd = new SqlCommand(oString, myConnection);
myConnection.Open();
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{
Product.ProductName= oReader["ProductName"].ToString();
Product.CategoryI = oReader["CategoryI "].ToString();
}
myConnection.Close();
}
}
var queryAllProduct = from p in Product
select p;
What is the best practice?
To not use a stored procedure.
The performance benefit of precompiled stored procedures was eliminated with SQL Server 7 (yes, that is 7 - 25 years or so ago, IIRC). Since then plans are cached and reused for dynamic SQL. Now stored procedures make only sense if
The SQL is VERY large and he result very slow (reducing network traffic) and the network is slow.
You do complex processing that requires brutal data transfers otherwise.
In fact, in your particular case I would use an ORM and LINQ and not waste my time manually writing SQL code (that easily accounts for 40% of a programs code) that can instead be generated.

I Want to Load Store Procedure Without Using EF [closed]

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 5 years ago.
Improve this question
Load the Store Procedure without using EF I Faced this Problem. I want to load the SP list type
Please see the sample below. Its also shows how to read the records from the StoredProcedure execution in your C# Code.
Also its a good practice initiating the SQLConnection and SQLCommand object in Using Block.
using (var conn = new SqlConnection(cnnString))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "SearchCustomer";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
// Use below line if you want to pass any parameter values to SP.
// cmd.Parameters.AddWithValue("#id", CustomerId);
using (var reader = cmd.ExecuteReader())
{
while(reader.Read())
{
// Read Column based on Column Name. Below sample reads String column
Console.WriteLine(reader.GetString(reader.GetOrdinal("columnName"));
// Read Column based on Column Index. Below sample reads int column
Console.WriteLine(reader.GetInt32(1));
}
}
}

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

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.

C# MySQL DataRow with multiple values [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 9 years ago.
Improve this question
Is there a way to do a c# datarow like the below code in a SELECT * query
//Have results like blow
string username = (string)row["username"];
I've tried but all I seem to see is reader or something, witch I know nothing about and don't understand. Can you lead me to some code that will help or give me a example?
DataReader is actually exactly what you need. The 'DataRow' class by itself won't help you; that gets used as part of a more complex solution, the 'DataSet' class (which uses 'DataTable' and that in turn uses 'DataColumn' and 'DataRow'). I don't see many people using 'DataSet'; if you want something complex with drag-and-drop design, you should look at using Entity Framework.
Here is a standard way to read values from SQL in .NET via DataReader (which, no matter what anyone says, is the fastest way to simply read data from a SQL database in .NET):
using (var connection = new SqlConnection("<Your connection string here>")
{
var command = new SqlCommand(
"SELECT username, email FROM users;",
connection);
connection.Open();
var reader = command.ExecuteReader(); // Using the DataReader (specifically, the SqlDataReader)
if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine("User {0} has email {1}", reader["username"],
reader["email"]);
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}
MSDN documentation for DataReader

Categories