日期:2014-05-18  浏览次数:20933 次

求助 哪错误
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace mdf
{
  class Program
  {
  static void Main(string[] args)
  {
  SqlConnection conn = new SqlConnection();
  conn.ConnectionString =@"Data source=.\;Initial Catalog=lianxi;User ID=sa;Password=fengjianhua";
  conn.Open();
  SqlCommand cmd = new SqlCommand();
  cmd.CommandText = "insert into fjh(name) values ('abf')";
  cmd.ExecuteNonQuery();----------------提示:ExecuteNonQuery: Connection 属性尚未初始化。
  Console.WriteLine("插入成功");
  Console.WriteLine("连接成功");
  Console.ReadKey();
  }
  }
}


------解决方案--------------------
conn.ConnectionString =@"Data source=.\;Initial Catalog=lianxi;User ID=sa;Password=fengjianhua";
==========
 conn.ConnectionString =@"Data source=.;Initial Catalog=lianxi;User ID=sa;Password=fengjianhua";
------解决方案--------------------
如果数据库是EXPRESS版本的话:
Data source=./sqlexpress
------解决方案--------------------
sorry

 SqlCommand cmd = new SqlCommand(conn );
------解决方案--------------------
SqlConnection conn = new SqlConnection();
conn.ConnectionString =@"Data source=.\;Initial Catalog=lianxi;User ID=sa;Password=fengjianhua";
=>

SqlConnection conn = new SqlConnection("server=.;database=lianxi;uid=sa;pwd=fengjianhua");
------解决方案--------------------
sorry

 SqlCommand cmd = new SqlCommand(conn );
------解决方案--------------------
改成这样:
C# code

conn.ConnectionString =@"Data source=(local);Initial Catalog=lianxi;User ID=sa;Password=fengjianhua";

------解决方案--------------------
SqlConnection conn = new SqlConnection();
conn.ConnectionString =@"Data source=.;Initial Catalog=lianxi;User ID=sa;Password=fengjianhua";
conn.Open();
SqlCommand cmd = new SqlCommand("insert into fjh(name) values ('abf')",conn); cmd.ExecuteNonQuery();
Console.WriteLine("插入成功");
Console.WriteLine("连接成功");
Console.ReadKey();