ExecuteNonQuery: Connection 属性尚未初始化
这个问题搞了好久,就是我在使用sqlcommand数据插入出现的问题,下面是代码:下面是触发command事件后,所触发的事件!Stringsqlconn="server...
这个问题搞了好久,就是我在使用sqlcommand数据插入出现的问题,下面是代码:下面是触发command事件后,所触发的事件!
String sqlconn = "server=192.168.2.9; uid=sa; pwd=123456; database=Example; Trusted_Connection=no";
SqlConnection myConnection = new SqlConnection(sqlconn);
myConnection.Open();
SqlCommand myCommand = new SqlCommand();
myCommand .CommandText = "insert into dbo.rigister(姓名,性别,年龄,祖籍,现在所住地,移动电话,固定电话,邮箱,求职岗位,学历,毕业院校,技能,工作经验,自我评定)VALUES (@name,@sex,@age,@homeplace,@nowplace,@mobilephone,@fixphone,@email,@profile,@education,@graduation,@skill,@expirence,@remark)";
SqlParameter[] par = new SqlParameter[]
{
new SqlParameter("@name",SqlDbType.VarChar,50),
new SqlParameter("@sex",SqlDbType.VarChar,12),
new SqlParameter("@age",SqlDbType.VarChar,32),
new SqlParameter("@homeplace",SqlDbType.VarChar,50),
new SqlParameter("@nowplace",SqlDbType.VarChar,50),
new SqlParameter("@mobilephone",SqlDbType.VarChar,50),
new SqlParameter("@fixphone",SqlDbType.VarChar,50),
new SqlParameter("@email",SqlDbType.VarChar,50),
new SqlParameter("@profile",SqlDbType.VarChar,50),
new SqlParameter("@education",SqlDbType.VarChar,50),
new SqlParameter("@graduation",SqlDbType.VarChar,50),
new SqlParameter("@skill",SqlDbType.VarChar,50),
new SqlParameter("@expirence",SqlDbType.VarChar,500),
new SqlParameter("@remark",SqlDbType.VarChar,500),
};
par[0].Value = TextBox2.Text;
par[1].Value = TextBox3.Text;
par[2].Value = TextBox4.Text;
par[3].Value = TextBox5.Text;
par[4].Value = TextBox6.Text;
par[5].Value = TextBox7.Text;
par[6].Value = TextBox8.Text;
par[7].Value = TextBox9.Text;
par[8].Value = TextBox10.Text;
par[9].Value = TextBox11.Text;
par[10].Value = TextBox12.Text;
par[11].Value = TextBox13.Text;
par[12].Value = TextBox14.Text;
par[13].Value = TextBox15.Text;
//循环压参数
for (int i = 1; i < 14; i++)
{
myCommand.Parameters.Add(par[i]);
}
myCommand.ExecuteNonQuery();
myConnection.Close();
Response.Write("<script>alert('新用户注册成功!');</script>");
} 展开
String sqlconn = "server=192.168.2.9; uid=sa; pwd=123456; database=Example; Trusted_Connection=no";
SqlConnection myConnection = new SqlConnection(sqlconn);
myConnection.Open();
SqlCommand myCommand = new SqlCommand();
myCommand .CommandText = "insert into dbo.rigister(姓名,性别,年龄,祖籍,现在所住地,移动电话,固定电话,邮箱,求职岗位,学历,毕业院校,技能,工作经验,自我评定)VALUES (@name,@sex,@age,@homeplace,@nowplace,@mobilephone,@fixphone,@email,@profile,@education,@graduation,@skill,@expirence,@remark)";
SqlParameter[] par = new SqlParameter[]
{
new SqlParameter("@name",SqlDbType.VarChar,50),
new SqlParameter("@sex",SqlDbType.VarChar,12),
new SqlParameter("@age",SqlDbType.VarChar,32),
new SqlParameter("@homeplace",SqlDbType.VarChar,50),
new SqlParameter("@nowplace",SqlDbType.VarChar,50),
new SqlParameter("@mobilephone",SqlDbType.VarChar,50),
new SqlParameter("@fixphone",SqlDbType.VarChar,50),
new SqlParameter("@email",SqlDbType.VarChar,50),
new SqlParameter("@profile",SqlDbType.VarChar,50),
new SqlParameter("@education",SqlDbType.VarChar,50),
new SqlParameter("@graduation",SqlDbType.VarChar,50),
new SqlParameter("@skill",SqlDbType.VarChar,50),
new SqlParameter("@expirence",SqlDbType.VarChar,500),
new SqlParameter("@remark",SqlDbType.VarChar,500),
};
par[0].Value = TextBox2.Text;
par[1].Value = TextBox3.Text;
par[2].Value = TextBox4.Text;
par[3].Value = TextBox5.Text;
par[4].Value = TextBox6.Text;
par[5].Value = TextBox7.Text;
par[6].Value = TextBox8.Text;
par[7].Value = TextBox9.Text;
par[8].Value = TextBox10.Text;
par[9].Value = TextBox11.Text;
par[10].Value = TextBox12.Text;
par[11].Value = TextBox13.Text;
par[12].Value = TextBox14.Text;
par[13].Value = TextBox15.Text;
//循环压参数
for (int i = 1; i < 14; i++)
{
myCommand.Parameters.Add(par[i]);
}
myCommand.ExecuteNonQuery();
myConnection.Close();
Response.Write("<script>alert('新用户注册成功!');</script>");
} 展开
4个回答
展开全部
SqlCommand myCommand = new SqlCommand();
中的SqlCommand(); ,是不是少了个参数??你只有myCommand .CommandText,那另一个的数据库的连接串呢??是不是忘写了??
我一般这个地方会这样写,省的忘:
SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString = "server=192.168.2.9; uid=sa; pwd=123456; database=Example";
string stSql="insert into dbo.rigister(姓名,性别,年龄,祖籍,现在所住地,移动电话,固定电话,邮箱,求职岗位,学历,毕业院校,技能,工作经验,自我评定)VALUES (@name,@sex,@age,@homeplace,@nowplace,@mobilephone,@fixphone,@email,@profile,@education,@graduation,@skill,@expirence,@remark)";
SqlCommand comm = new SqlCommand(sql, sqlconn);
SqlCommand myCommand = new SqlCommand(stSql,sqlconn);
我也好久没写过这样的语句了,也不知道我说的是对还是错。。没有环境不能调试。如果错了,不要骂我哦,呵呵
中的SqlCommand(); ,是不是少了个参数??你只有myCommand .CommandText,那另一个的数据库的连接串呢??是不是忘写了??
我一般这个地方会这样写,省的忘:
SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString = "server=192.168.2.9; uid=sa; pwd=123456; database=Example";
string stSql="insert into dbo.rigister(姓名,性别,年龄,祖籍,现在所住地,移动电话,固定电话,邮箱,求职岗位,学历,毕业院校,技能,工作经验,自我评定)VALUES (@name,@sex,@age,@homeplace,@nowplace,@mobilephone,@fixphone,@email,@profile,@education,@graduation,@skill,@expirence,@remark)";
SqlCommand comm = new SqlCommand(sql, sqlconn);
SqlCommand myCommand = new SqlCommand(stSql,sqlconn);
我也好久没写过这样的语句了,也不知道我说的是对还是错。。没有环境不能调试。如果错了,不要骂我哦,呵呵
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
启帆信息
2024-11-19 广告
2024-11-19 广告
启帆信息是英伟达中国区代理商,原厂授权代理,提供全面的软件技术解决方案以及NVIDIA以太网产品、交换机等产品,欢迎前来咨询!...
点击进入详情页
本回答由启帆信息提供
展开全部
没有给command对象指定相应参数
myCommand.Connection = con;
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = strSql;
我给你一个DB类:
public class DB
{
//数据库连接字符串
public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//声明数据库连接对象
protected SqlConnection con;
/// <summary>
/// 默认构造函数,在构造函数中打开数据库连接
/// </summary>
public LotteryDB()
{
//在创建SqlConnection对象时传入连接字符串
con = new SqlConnection(ConnectionString);
//打开连接
con.Open();
}
/// <summary>
/// 进行添加、更新和删除的通用方法,即此方法可执行除SELECT以外的相应的INSERT、UPDATE和DELETE方法
/// </summary>
/// <param name="strSql">所执行的SQL语句</param>
/// <param name="cmdParms">所执行SQL语句中的相应参数</param>
/// <returns>返回一个INT类型的值,如果是1则执行成功反之失败</returns>
public int RunLottery(string strSql,SqlParameter[] cmdParms)
{
//启动一个事务
SqlTransaction tran = con.BeginTransaction();
try
{
//判断连接状态是否处于打开,如果不是打开连接
if (con.State != ConnectionState.Open)
{
con.Open();
}
//创建SqlCommand对象
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql;
//判断cmd参数是否为空,如果不为空则传入参数
if (cmdParms != null)
{
//遍历SqlParameter数组将其传递进cmd对象中
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
//获得运行时的事务
cmd.Transaction = tran;
//执行数据库操作并返回一个整型值,通过此值来判断操作是否成功
int val = cmd.ExecuteNonQuery();
//清空SqlCommand参数
cmd.Parameters.Clear();
//提交事务
tran.Commit();
//返回整型值
return val;
}
catch
{
//如果在程序运行时出现异常,关闭连接
con.Close();
//事务回滚
tran.Rollback();
//抛出异常信息
throw;
}
}
/// <summary>
/// 通用查询方法
/// </summary>
/// <param name="strSql">SELECT语句</param>
/// <param name="cmdParms">SELECT语句中的参数</param>
/// <returns>返回Sqldatareader对象</returns>
public SqlDataReader SelectLottery(string strSql, SqlParameter[] cmdParms)
{
try
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
//将执行查询语句后的集合赋值给SqlDataReader对象
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
//返回SqlDataReader对象
return rdr;
}
catch
{
con.Close();
throw;
}
}
}
myCommand.Connection = con;
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = strSql;
我给你一个DB类:
public class DB
{
//数据库连接字符串
public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//声明数据库连接对象
protected SqlConnection con;
/// <summary>
/// 默认构造函数,在构造函数中打开数据库连接
/// </summary>
public LotteryDB()
{
//在创建SqlConnection对象时传入连接字符串
con = new SqlConnection(ConnectionString);
//打开连接
con.Open();
}
/// <summary>
/// 进行添加、更新和删除的通用方法,即此方法可执行除SELECT以外的相应的INSERT、UPDATE和DELETE方法
/// </summary>
/// <param name="strSql">所执行的SQL语句</param>
/// <param name="cmdParms">所执行SQL语句中的相应参数</param>
/// <returns>返回一个INT类型的值,如果是1则执行成功反之失败</returns>
public int RunLottery(string strSql,SqlParameter[] cmdParms)
{
//启动一个事务
SqlTransaction tran = con.BeginTransaction();
try
{
//判断连接状态是否处于打开,如果不是打开连接
if (con.State != ConnectionState.Open)
{
con.Open();
}
//创建SqlCommand对象
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql;
//判断cmd参数是否为空,如果不为空则传入参数
if (cmdParms != null)
{
//遍历SqlParameter数组将其传递进cmd对象中
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
//获得运行时的事务
cmd.Transaction = tran;
//执行数据库操作并返回一个整型值,通过此值来判断操作是否成功
int val = cmd.ExecuteNonQuery();
//清空SqlCommand参数
cmd.Parameters.Clear();
//提交事务
tran.Commit();
//返回整型值
return val;
}
catch
{
//如果在程序运行时出现异常,关闭连接
con.Close();
//事务回滚
tran.Rollback();
//抛出异常信息
throw;
}
}
/// <summary>
/// 通用查询方法
/// </summary>
/// <param name="strSql">SELECT语句</param>
/// <param name="cmdParms">SELECT语句中的参数</param>
/// <returns>返回Sqldatareader对象</returns>
public SqlDataReader SelectLottery(string strSql, SqlParameter[] cmdParms)
{
try
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
//将执行查询语句后的集合赋值给SqlDataReader对象
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
//返回SqlDataReader对象
return rdr;
}
catch
{
con.Close();
throw;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SqlCommand myCommand = new SqlCommand();
后面加上
myCommand.Connection = myConnection;
后面加上
myCommand.Connection = myConnection;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的Command 没有和Connection关联
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询