C#连接数据库sqlserver2005,并执行sql语句实例:
[csharp] view plaincopy- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data.SqlClient;
-
- namespace DBdemo1
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- string connstring = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=mapdemo;Data Source=PC-20130306BGML";
-
- string sqlstring = "update students set studentname = 'zhangsan' where studentID = '123456'";
-
-
- SqlConnection conn = new SqlConnection(connstring);
- conn.Open();
-
- SqlCommand cmd = new SqlCommand(sqlstring, conn);
-
-
- int rowsreturned = cmd.ExecuteNonQuery();
-
- Console.WriteLine(rowsreturned);
-
-
- conn.Close();
-
-
- }
- }
- }