本文简单的介绍的通过C#对数据库的读写操作。
请先参考一下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string connectonString = "Data Source=(local);Initial Catalog=test; Integrated Security=SSPI;";
string commandString = "insert into test.dbo.student values ('100002','wei song',22,'3','Liu','test from sql')";
string commansString_Parameter = "insert into test.dbo.student values (@id,@name,22,'3','Liu','test from sql')";
string commansString_Parameter_ext = "insert into test.dbo.student values (@id_ext,@name_ext,22,'3','Liu','test from sql')";
SqlConnection sqlConnection = new SqlConnection(connectonString);
sqlConnection.Open();
Console.WriteLine(sqlConnection.ServerVersion);
Console.WriteLine(sqlConnection.State);
// write the sql command , use sqlcommand to insert data to the datebase.
SqlCommand command = new SqlCommand();
command.Connection = sqlConnection;
command.CommandText = commandString;
command.ExecuteNonQuery();
Console.WriteLine(sqlConnection.State);
// add parameters to the sql comma