C# 连接数据库的问题 很急 在线等 谢谢
小弟初学C#和SQL,看的是杨中科老师的视频,采用内置MDF文件的方式连接数据库,但是SQL语句执行成功,返回受影响的行数,表却没有任何变化,不知道为什么?求解?是不是需要装VS2008 SP1补丁之类的?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace MDF
{
class Program
{
static void Main(string[] args)
{
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
}
using (SqlConnection sqlConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
{
sqlConnection.Open(); //打开连接
Console.WriteLine("连接数据库成功");
using (SqlCommand sqlCmd = sqlConnection.CreateCommand())
{
sqlCmd.CommandText = "insert into Table1(ID,Name) values(1,'jia')";
Console.WriteLine("插入数据成功!");
}
}
Console.ReadKey();
}
}
}
谢谢
------解决方案--------------------
sqlCmd.CommandText = "insert into Table1(ID,Name) values(1,'jia')";
if(sqlCmd.ExecuteNonQuery() > 0)
{
Console.WriteLine("插入数据成功!");
}