肯定是连接问题。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);
一定是连接时,没有最好一句。
sqlcon.Open();
SqlCommand cmd = new SqlCommand(s_sql, sqlcon);
SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(cmd);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
一定是连接时,没有最好一句。
*******数据库的存储过程名*******
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
commin tran
应该是
commit tran
-----------------------------------------------------------
爱找房(http://www.izfang.com)
我的个人网站,免费的房屋租赁网站,大家要多多捧场哦。
SQL Sever的存储过程我没写过,但我想应该大同小异吧。
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;
} }
}