肯定是连接问题。SqlConnection  sqlcon = new SqlConnection(s_con);
sqlcon.Open();
SqlCommand cmd = new SqlCommand(s_sql, sqlcon); 
SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(cmd);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);  
一定是连接时,没有最好一句。

解决方案 »

  1.   

    在存储过程里,做一下事务提交试试看
    *******数据库的存储过程名*******  
    create procedure [dbo].[Room_Add]  
    (  
       @roomId int,  
       @roomTypeName nvarchar(100),  
       @roomPosition nvarchar(100),  
       @holdPeopleNum int ,  
       @bedNum int,  
       @re nvarchar(100)  
    )  
    As  
    begin tran   
    Insert into Room (roomId ,roomTypeName ,roomPosition ,holdPeopleNum ,bedNum ,re)  
    values(@roomId ,@roomTypeName ,@roomPosition ,@holdPeopleNum ,@bedNum ,@re); 
    commin tran
      

  2.   

    上面我最后一句写错了,不是
      commin tran
    应该是
      commit tran
    ----------------------------------------------------------- 
        爱找房(http://www.izfang.com
     我的个人网站,免费的房屋租赁网站,大家要多多捧场哦。
      

  3.   

    我在写Oracle的存储过程时,如果涉及数据插入和更新的话,每次都要求在最后加上Commit事务提交命令才能进行数据插入和更新。
    SQL Sever的存储过程我没写过,但我想应该大同小异吧。
      

  4.   

    数据封装函数为
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    namespace HotelManagers.DBConnection
    {
        /// <summary>
        /// 数据库的封装函数
        /// </summary>
        public class DB
        {
            public static int ExectueNoQuery(string storeProcedureName, SqlParameter[] parms)
            {
                SqlConnection conn = db2.conString();
                SqlCommand cmd = new SqlCommand();
                int x;
                try
                {
                    conn.Open();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = storeProcedureName;
                    for (int count = 0; count < parms.GetLength(0); count++)
                    {
                        cmd.Parameters.Add(parms[count]);
                    }
                    x = cmd.ExecuteNonQuery();            }
                catch (Exception)
                {
                    return -1;
                }
                finally
                {
                    cmd.Dispose();
                    conn.Close();
                    conn.Dispose();
                }
                return x;        }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="storeProcedureName"></param>
            /// <param name="parms"></param>
            /// <returns></returns>
            public static DataSet ExecuteQuery(string storeProcedureName, SqlParameter[] parms)
            {
                SqlConnection conn = db2.conString();
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet myDs = new DataSet();            try
                {
                    conn.Open();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = storeProcedureName;
                    for (int counter = 0; counter < parms.GetLength(0); counter++)
                    {
                        cmd.Parameters.Add(parms[counter]);
                    }
                    da.SelectCommand = cmd;
                    da.Fill(myDs);
                }
                catch
                {
                    return null;
                }
                finally
                {
                    da.Dispose();
                    cmd.Dispose();
                    conn.Dispose();
                    conn.Close();            }
                return myDs;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="storeProcedureName"></param>
            /// <returns></returns>
            public static DataSet ExecuteQuery(string storeProcedureName)
            {
                SqlConnection conn = db2.conString();
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet myDs = new DataSet();            try
                {
                    conn.Open();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = storeProcedureName;                da.SelectCommand = cmd;
                    da.Fill(myDs);
                }
                catch
                {
                    return null;
                }
                finally
                {
                    da.Dispose();
                    cmd.Dispose();
                    conn.Dispose();
                    conn.Close();            }
                return myDs;
            }    }
    }