下面SQL语句在C#执行显示连接失败,但是在数据库上却添加成功了
register();stringconn=string.Format("DataSource='{0}';InitialCatalog='{1}';UserID=sa;...
register();
string conn = string.Format("Data Source='{0}';Initial Catalog='{1}';User ID=sa;Password='{2}'", st.a, st.b, st.d);
SqlConnection cn = new SqlConnection(conn);//sql连接;
try
{
cn.Open();
st.fla = true;
string sql1 = "insert into [User] (UserName,UserAge,UserSex,UserPhone,UserAdss)values ('"+st.userName+"',"+st.userAge+",'"+st.userSex+"','"+st.userPhone+"','"+st.userAdss+"')";
string sql2 = "insert into Administrator (Account,Accpws,UserName) values ('" + st.userID + "','" + st.userPws + "','" + st.userName + "')";
SqlCommand com = new SqlCommand(sql1, cn);
SqlCommand ccm = new SqlCommand(sql2, cn);
int iRe = (int)com.ExecuteScalar();
int iTe = (int)ccm.ExecuteScalar();
}
catch (Exception)
{
Console.WriteLine("连接失败!");
st.fla = false;
}
finally
{
//关闭数据库连接
cn.Close();
} 展开
string conn = string.Format("Data Source='{0}';Initial Catalog='{1}';User ID=sa;Password='{2}'", st.a, st.b, st.d);
SqlConnection cn = new SqlConnection(conn);//sql连接;
try
{
cn.Open();
st.fla = true;
string sql1 = "insert into [User] (UserName,UserAge,UserSex,UserPhone,UserAdss)values ('"+st.userName+"',"+st.userAge+",'"+st.userSex+"','"+st.userPhone+"','"+st.userAdss+"')";
string sql2 = "insert into Administrator (Account,Accpws,UserName) values ('" + st.userID + "','" + st.userPws + "','" + st.userName + "')";
SqlCommand com = new SqlCommand(sql1, cn);
SqlCommand ccm = new SqlCommand(sql2, cn);
int iRe = (int)com.ExecuteScalar();
int iTe = (int)ccm.ExecuteScalar();
}
catch (Exception)
{
Console.WriteLine("连接失败!");
st.fla = false;
}
finally
{
//关闭数据库连接
cn.Close();
} 展开
5个回答
展开全部
你把代码改成这样:
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
{
/// <summary>
/// 此类维护数据库连接字符串,和 Connection 对象
/// </summary>
public class DBHelper
{
// 数据库连接字符串
private string connString = @"Data Source=.\SQLEXPRESS;Initial Catalog=MySchool;User ID=FrenziedDevil;Password=123";
// 数据库连接 Connection 对象
private SqlConnection connection;
/// <summary>
/// Connection对象
/// </summary>
public SqlConnection Connection
{
get
{
if (connection == null)
{
connection = new SqlConnection(connString);
}
return connection;
}
}
/// <summary>
/// 打开数据库连接
/// </summary>
public void OpenConnection()
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
else if (Connection.State == ConnectionState.Broken)
{
Connection.Close();
Connection.Open();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
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。。。。。
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
{
/// <summary>
/// 此类维护数据库连接字符串,和 Connection 对象
/// </summary>
public class DBHelper
{
// 数据库连接字符串
private string connString = @"Data Source=.\SQLEXPRESS;Initial Catalog=MySchool;User ID=FrenziedDevil;Password=123";
// 数据库连接 Connection 对象
private SqlConnection connection;
/// <summary>
/// Connection对象
/// </summary>
public SqlConnection Connection
{
get
{
if (connection == null)
{
connection = new SqlConnection(connString);
}
return connection;
}
}
/// <summary>
/// 打开数据库连接
/// </summary>
public void OpenConnection()
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
else if (Connection.State == ConnectionState.Broken)
{
Connection.Close();
Connection.Open();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
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;
}
这样会显示更具体的异常信息
问题可能出现在这里.
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;
}
catch (Exception)
{
Console.WriteLine("连接失败!");
st.fla = false;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
像楼上的那样把异常打印出来,贴上来,我们再来解决。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
应该是你连接数据库的语句出现问题了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询