C#中如何连接数据库并执行SQL语句
1个回答
展开全部
using System.Data.SqlClient;
SqlConnection conn = new SqlConnection();
string connectionString="server=.;database=Sql;uid=sa; pwd=123456";
conn.ConnectionString = connectionString;
conn.open();
string sqlStr = "SELECT * FROM table1";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sqlStr;
cmd.CommandType = CommandType.Text;
int i = Convert.ToInt32(cmd.ExecuteNonQuery());
Console.Write("共有" + i.ToString() + "条数据");
conn.Close();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询