Из почты: как читать данные из SQL Server из C#?
??? ????? ??? ?????? ?????? ???????. ??????-??, ?????? ???????????, ?? ? ?????? ???????, ?????? ?? ? ????
_______________________________________
From: <sandusergiu1984@....com> [mailto:sandusergiu1984@....com]
Sent: Fri 8/11/2006 9:20 AM
To: Eldar Musayev
Subject: (Random Thoughts and Hints on Software Development) : C# Code
I thought how can I write a program in C# that searches a SQL Server database and outputs the results to the user? and couldn't give me please some ideas, or a fragment of code? Thanks a lot.
----------------------------------
This message was generated from a contact form at: https://blogs.msdn.com/eldar/default.aspx
????, ??? ???. ??? ??????, ???? Class1.cs (??? ???????, ??????? ??????? ???, ??????? ??????? ????? ? ???? ??????):
using System;
namespace GetData
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Starting...");
MyDataReader.InsertRow(73,"??????");
Console.WriteLine("Insert is done.");
MyDataReader.ReadMyData();
Console.WriteLine("Press any key to continiue...");
Console.ReadLine();
Console.WriteLine("... done.");
}
}
}
? ?????? ??? ???, ??????? ?? ????????:
using System;
using System.Data.SqlClient;namespace GetData
{
/// <summary>
///
/// </summary>
public class MyDataReader
{
public MyDataReader()
{
}// SQLEXPRESS is used for
// the free SQL Express edition of SQL Server
private static String myConnectionString = "Database=sample;Server=MyMachine\\SQLEXPRESS;Integrated Security=SSPI;";public static void InsertRow(int id, string str)
{
try
{
SqlConnection myConnection = new SqlConnection(myConnectionString);
string myInsertQuery = "INSERT INTO SampleTable (id, str) Values('"+id.ToString()+"', '"+str+"')";
SqlCommand myCommand = new SqlCommand(myInsertQuery);
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}public static void ReadMyData()
{
string mySelectQuery = "SELECT id, str FROM SampleTable";
SqlConnection myConnection = new SqlConnection(myConnectionString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
try
{
while (myReader.Read())
{
Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
}
}
finally
{
// always call Close when done reading.
myReader.Close();
// always call Close when done reading.
myConnection.Close();
}
}}
}
??, ? ??? ??? ?????? (????? ? ???? ??????) - ??? ????? ReadMyData(). ???????? ????? C# command-line ?????? ? Visual Studio, ???????? ???? ??? ????? (CLass1.cs ?????? ????, ??????? ??????? VS), ?????????? ?????? ?????????? (???????? ???? ?????? ? ??? SQL Server'? - SQLEXPRESS - ??? ??? ?????????? ?????? SQL Server Express), ?? ???????? ??????? ???? ?????? sample ? ? ??? ??????? ? ????? ?????? - id ? str, ????????????? ??????, ? ?????? ??? ?????????.
?? ????????, ??? ???? ?? ????????? ???????????? SQL Server ?? ?????? ??????, ?? ?? ?????? ???? ????????, ????? ????????? ??????? ?? ?????? ?? ????????? ??????. ? ?? ???????? ???????? ?? ??? Windows authentication -- ??? ??, ? ??? ??????? SSPI ? ?????? ??????????.
?????!