你把代码改成这样:
try
{
cn.Open();
st.fla = true;
StringBuilder sb_1 = new StringBuilder();
sb_1.AppendLine(" insert");
sb_1.AppendLine(" [User]([UserName],[UserAge],[UserSex],[UserPhone],[UserAdss])");
sb_1.AppendLine(" values");
sb_1.AppendLine(" ('" + st.userName+ "'," + st.userAge+ ",'" + userSex + "','"+st.userPhone+"','"+st.userAdss+"')");
SqlCommand comm = new SqlCommand(sb_1.ToString(), cn);
int iRe = (int)comm.ExecuteNonQuery();
StringBuilder sb_2 = new StringBuilder();
sb_2.AppendLine(" insert");
sb_2.AppendLine(" [Administrator]([Account],[Accpws],[UserName])");
sb_2.AppendLine(" values");
sb_2.AppendLine(" ('" + st.userID + "','" + st.userPws + "','" + st.userName + "')");
comm = new SqlCommand(sb_2.ToString(), cn);
int iTe = (int)comm.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("系统出现异常!" + ex + "", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
st.fla = false;
}
finally
{
cn.Close();
}
若再不行,把你的数据库帮助类换成我这个:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;//SQL Server .NET 数据提供程序命名空间
namespace TestSeach
{
///
/// 此类维护数据库连接字符串,和 Connection 对象
///
public class DBHelper
{
// 数据库连接字符串
private string connString = @"Data Source=.\SQLEXPRESS;Initial Catalog=MySchool;User ID=FrenziedDevil;Password=123";
// 数据库连接 Connection 对象
private SqlConnection connection;
///
/// Connection对象
///
public SqlConnection Connection
{
get
{
if (connection == null)
{
connection = new SqlConnection(connString);
}
return connection;
}
}
///
/// 打开数据库连接
///
public void OpenConnection()
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
else if (Connection.State == ConnectionState.Broken)
{
Connection.Close();
Connection.Open();
}
}
///
/// 关闭数据库连接
///
public void CloseConnection()
{
if (Connection.State == ConnectionState.Open || Connection.State == ConnectionState.Broken)
{
Connection.Close();
}
}
}
}
然后把你的代码改成这样:
try
{
db.OpenConnection();
st.fla = true;
StringBuilder sb_1 = new StringBuilder();
sb_1.AppendLine(" insert");
sb_1.AppendLine(" [User]([UserName],[UserAge],[UserSex],[UserPhone],[UserAdss])");
sb_1.AppendLine(" values");
sb_1.AppendLine(" ('" + st.userName+ "'," + st.userAge+ ",'" + userSex + "','"+st.userPhone+"','"+st.userAdss+"')");
SqlCommand comm = new SqlCommand(sb_1.ToString(), db.Connection);
int iRe = (int)comm.ExecuteNonQuery();
StringBuilder sb_2 = new StringBuilder();
sb_2.AppendLine(" insert");
sb_2.AppendLine(" [Administrator]([Account],[Accpws],[UserName])");
sb_2.AppendLine(" values");
sb_2.AppendLine(" ('" + st.userID + "','" + st.userPws + "','" + st.userName + "')");
comm = new SqlCommand(sb_2.ToString(), db.Connection);
int iTe = (int)comm.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("系统出现异常!" + ex + "", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
db.CloseConnection();
}
记得在你的代码引用处加这段代码:
using System.Data;//(这个有的话可不要)
using System.Data.SqlClient;//SQL Server .NET 数据提供程序命名空间
好了,OK。。。。。
如果说在数据库中执行成功了,那就是执行insert语句ok,
问题可能出现在这里.
int iRe = (int)com.ExecuteScalar();
int iTe = (int)ccm.ExecuteScalar();
请将
catch (Exception)
{
Console.WriteLine("连接失败!");
st.fla = false;
}
修改为
catch(Exception error)
{
Console.WriteLine("连接失败!"+error.Message);
st.fla = false;
}
这样会显示更具体的异常信息
这种现象说明异常可能不是来源于数据库的操作,你最好把异常信息打印出来,看一下具体出错的位置,你在调试中用如下代码是不利于错误分析的,因为把真正的错误屏蔽了
catch (Exception)
{
Console.WriteLine("连接失败!");
st.fla = false;
}
像楼上的那样把异常打印出来,贴上来,我们再来解决。。。
应该是你连接数据库的语句出现问题了