Sql ExecuteOnQuery未执行-vb.net
我正在尝试执行一个SQL命令,但我就是不知道为什么这不起作用。 这就是我在名为“clsSQL”的类中定义“execute”函数的方式: 我这样称呼它:Sql ExecuteOnQuery未执行-vb.net,sql,vb.net,.net-2.0,Sql,Vb.net,.net 2.0,我正在尝试执行一个SQL命令,但我就是不知道为什么这不起作用。 这就是我在名为“clsSQL”的类中定义“execute”函数的方式: 我这样称呼它: Using oSQL As New clsSQL(My.Settings.projectConnectionString) If oSQL.OpenConnection Then strSQL = "INSERT INTO ... blablabla..."
Using oSQL As New clsSQL(My.Settings.projectConnectionString)
If oSQL.OpenConnection Then
strSQL = "INSERT INTO ... blablabla..."
oSQL.Execute(strSQL)
End If
End Using
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Using oSQL As New clsSQL(My.Settings.projectConnectionString)
If oSQL.OpenConnection Then
oSQL.ToDataGrid(Me.DataGridView, "select * from table")
End If
End Using
End Sub
代码没有引起错误,只是没有将数据保存在数据库中。错误不在SQL命令中,我已经手动测试了;)
例如,我可以完美地执行下一个函数,而不会出现任何问题:
Public Function ToDataGrid(ByVal oDataGrid As DataGridView, _
ByVal sQuery As String, _
Optional ByVal sTable As String = "") As Boolean
Try
#If DEBUG_MODE Then
Debug.WriteLine(sQuery)
#End If
Dim objDataSet As New DataSet
objDataSet = ToDataSet(sQuery, sTable)
oDataGrid.DataSource = objDataSet.Tables(0)
objDataSet.Dispose()
objDataSet = Nothing
Return True
Catch ex As Exception
RaiseEvent OnError("ToDataGrid", ex)
End Try
End Function
我这样称呼它:
Using oSQL As New clsSQL(My.Settings.projectConnectionString)
If oSQL.OpenConnection Then
strSQL = "INSERT INTO ... blablabla..."
oSQL.Execute(strSQL)
End If
End Using
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Using oSQL As New clsSQL(My.Settings.projectConnectionString)
If oSQL.OpenConnection Then
oSQL.ToDataGrid(Me.DataGridView, "select * from table")
End If
End Using
End Sub
也许,我只需要另一双眼睛,因为我看不出我做错了什么:|
谢谢我不确定这是否是错误的原因,只是想知道您在哪里设置了这个变量
m_objConnection
为了调试,您可以注释try/catch语句。IMO我不确定这是否是错误的原因,只是好奇您在哪里设置了这个变量
m_objConnection
为了调试,您可以注释try/catch语句。IMO您的代码看起来不错,您能给我们看一下INSERT语句吗。您是否也尝试过使用SQL Profiler查看插入是否命中数据库 下面是我不久前编写的一个SQL类,您可以使用它:
using System;
using System.Data.SqlClient;
namespace SAPCommonData
{
public class SQLClass : IDisposable
{
private String connString;
private SqlCommand SQLCmd;
private SqlConnection SQLConn;
public void Dispose()
{
if (SQLCmd != null)
{
SQLCmd.Dispose();
SQLCmd=null;
}
if (SQLConn != null)
{
SQLConn.Dispose();
SQLConn = null;
}
}
public String SQLConnString
{
get{ return connString; }
set{ connString = value; }
}
private String GetSQLConnString()
{
String strConn;
try
{
strConn = System.Configuration.ConfigurationSettings.AppSettings.Get("SQL_CONN");
}
catch (Exception ex)
{
throw (new System.Exception(ex.Message.ToString()));
}
return strConn;
}
public void SQLExecuteNonQuery()
{
if (SQLCmd == null)
{
throw (new System.Exception("Must use SetSQLCommand to initialize SQLCommand object!"));
}
if (SQLConn == null)
{
OpenSQLDB();
}
try
{
SQLCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw (new System.Exception(ex.Message.ToString()));
}
finally
{
SQLCmd.Dispose();
SQLCmd=null;
}
}
public SQLClass()
{
try {
connString = GetSQLConnString();
}
catch (Exception e)
{
throw new System.Exception(e.Message.ToString());
}
try
{
SQLConn = new SqlConnection(connString);
SQLConn.Open();
}
catch (Exception e)
{
throw (new System.Exception(e.Message.ToString()));
}
}
public void OpenSQLDB()
{
if (IsOpen())
{
//connection state open already
}
else
{
if (connString.Length == 0)
{
try
{
connString = GetSQLConnString();
}
catch (Exception e)
{
throw new System.Exception(e.Message.ToString());
}
}
try
{
SQLConn = new SqlConnection(connString);
SQLConn.Open();
}
catch (Exception e)
{
throw (new System.Exception(e.Message.ToString()));
}
}
}
public bool IsOpen()
{
return (SQLConn.State == System.Data.ConnectionState.Open);
}
public void CloseSQLDB()
{
if (IsOpen())
{
SQLConn.Dispose();
SQLConn.Close();
SQLConn=null;
}
}
public String SQLDBUsed()
{
return SQLConn.Database;
}
public void SetSQLCommand(String proc)
{
if (proc.Length == 0)
{
throw new System.Exception("Procedure must be specified when calling SetSQLCommand!");
}
if (SQLConn == null)
{
OpenSQLDB();
}
SQLCmd = new SqlCommand(proc, SQLConn);
SQLCmd.CommandType = System.Data.CommandType.StoredProcedure;
}
public void AddSQLCmdParameter(String pName, System.Data.SqlDbType pType, object pVal)
{
if (SQLCmd == null)
{
throw (new System.Exception("Must use SetSQLCommand to initialize SQLCommand object!"));
}
if (SQLConn == null)
{
OpenSQLDB();
}
try
{
SQLCmd.Parameters.Add(pName, pType).Value = pVal;
}
catch (Exception ex)
{
throw (new System.Exception(ex.Message.ToString()));
}
}
public void ClearSQLCmdParameters()
{
if (SQLCmd != null)
{
SQLCmd.Parameters.Clear();
}
}
public void DisposeSQLCmd()
{
if (SQLCmd != null)
{
SQLCmd.Dispose();
}
}
public System.Data.SqlClient.SqlDataReader SQLExecuteReader()
{
System.Data.SqlClient.SqlDataReader dr;
if (SQLCmd == null)
{
throw (new System.Exception("Must use SetSQLCommand to initialize SQLCommand object!"));
}
if (SQLConn == null)
{
OpenSQLDB();
}
try
{
dr = SQLCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw (new System.Exception(ex.Message.ToString()));
}
finally
{
SQLCmd.Dispose();
SQLCmd=null;
}
return dr;
}
}
}
然后可以像这样重用此对象:
public void ProcessCustomerData(DataTable dt)
{
//we have a valid connection to the database
if (dt.Rows.Count > 0)
{
try
{
s=new SQLClass();
}
catch (Exception e)
{
throw new System.Exception(e.Message.ToString());
}
foreach(DataRow dr in dt.Rows)
{
tw.WriteLine("Processing customer: " + dr["NAME1"].ToString());
Console.WriteLine("Processing customer: " + dr["NAME1"].ToString());
s.SetSQLCommand("insCustomer");
s.AddSQLCmdParameter("@ClientID", System.Data.SqlDbType.Int, dr["MANDT"]);
s.AddSQLCmdParameter("@CustomerID", System.Data.SqlDbType.BigInt, dr["KUNNR"]);
s.AddSQLCmdParameter("@CustomerName1", System.Data.SqlDbType.VarChar, ((string)dr["NAME1"]==String.Empty ? DBNull.Value : dr["NAME1"]));
s.AddSQLCmdParameter("@CustomerName2", System.Data.SqlDbType.VarChar, ((string)dr["NAME2"]==String.Empty ? DBNull.Value : dr["NAME2"]));
s.AddSQLCmdParameter("@Country", System.Data.SqlDbType.VarChar, ((string)dr["LAND1"]==String.Empty ? DBNull.Value : dr["LAND1"]));
s.AddSQLCmdParameter("@Region", System.Data.SqlDbType.VarChar, ((string)dr["REGIO"]==String.Empty ? DBNull.Value : dr["REGIO"]));
s.AddSQLCmdParameter("@City", System.Data.SqlDbType.VarChar, ((string)dr["ORT01"]==String.Empty ? DBNull.Value : dr["ORT01"]));
s.AddSQLCmdParameter("@ZipCode", System.Data.SqlDbType.VarChar, ((string)dr["PSTLZ"]==String.Empty ? DBNull.Value : dr["PSTLZ"]));
s.AddSQLCmdParameter("@Address", System.Data.SqlDbType.VarChar, ((string)dr["STRAS"]==String.Empty ? DBNull.Value : dr["STRAS"]));
s.AddSQLCmdParameter("@Telephone", System.Data.SqlDbType.VarChar, ((string)dr["TELF1"]==String.Empty ? DBNull.Value : dr["TELF1"]));
s.AddSQLCmdParameter("@Fax", System.Data.SqlDbType.VarChar, ((string)dr["TELFX"]==String.Empty ? DBNull.Value : dr["TELFX"]));
s.AddSQLCmdParameter("@DateAdded", System.Data.SqlDbType.DateTime, System.DateTime.Today);
s.AddSQLCmdParameter("@DateModified", System.Data.SqlDbType.DateTime, System.DateTime.Today);
s.AddSQLCmdParameter("@AddedBy", System.Data.SqlDbType.VarChar, DBNull.Value);
s.AddSQLCmdParameter("@ModifiedBy", System.Data.SqlDbType.VarChar, DBNull.Value);
s.SQLExecuteNonQuery();
Console.WriteLine("Processed customer: " + dr["NAME1"].ToString());
tw.WriteLine("Processed customer: " + dr["NAME1"].ToString());
}
s.CloseSQLDB();
s.Dispose();
}
}
你的代码看起来不错,你能给我们看一下INSERT语句吗。您是否也尝试过使用SQL Profiler查看插入是否命中数据库 下面是我不久前编写的一个SQL类,您可以使用它:
using System;
using System.Data.SqlClient;
namespace SAPCommonData
{
public class SQLClass : IDisposable
{
private String connString;
private SqlCommand SQLCmd;
private SqlConnection SQLConn;
public void Dispose()
{
if (SQLCmd != null)
{
SQLCmd.Dispose();
SQLCmd=null;
}
if (SQLConn != null)
{
SQLConn.Dispose();
SQLConn = null;
}
}
public String SQLConnString
{
get{ return connString; }
set{ connString = value; }
}
private String GetSQLConnString()
{
String strConn;
try
{
strConn = System.Configuration.ConfigurationSettings.AppSettings.Get("SQL_CONN");
}
catch (Exception ex)
{
throw (new System.Exception(ex.Message.ToString()));
}
return strConn;
}
public void SQLExecuteNonQuery()
{
if (SQLCmd == null)
{
throw (new System.Exception("Must use SetSQLCommand to initialize SQLCommand object!"));
}
if (SQLConn == null)
{
OpenSQLDB();
}
try
{
SQLCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw (new System.Exception(ex.Message.ToString()));
}
finally
{
SQLCmd.Dispose();
SQLCmd=null;
}
}
public SQLClass()
{
try {
connString = GetSQLConnString();
}
catch (Exception e)
{
throw new System.Exception(e.Message.ToString());
}
try
{
SQLConn = new SqlConnection(connString);
SQLConn.Open();
}
catch (Exception e)
{
throw (new System.Exception(e.Message.ToString()));
}
}
public void OpenSQLDB()
{
if (IsOpen())
{
//connection state open already
}
else
{
if (connString.Length == 0)
{
try
{
connString = GetSQLConnString();
}
catch (Exception e)
{
throw new System.Exception(e.Message.ToString());
}
}
try
{
SQLConn = new SqlConnection(connString);
SQLConn.Open();
}
catch (Exception e)
{
throw (new System.Exception(e.Message.ToString()));
}
}
}
public bool IsOpen()
{
return (SQLConn.State == System.Data.ConnectionState.Open);
}
public void CloseSQLDB()
{
if (IsOpen())
{
SQLConn.Dispose();
SQLConn.Close();
SQLConn=null;
}
}
public String SQLDBUsed()
{
return SQLConn.Database;
}
public void SetSQLCommand(String proc)
{
if (proc.Length == 0)
{
throw new System.Exception("Procedure must be specified when calling SetSQLCommand!");
}
if (SQLConn == null)
{
OpenSQLDB();
}
SQLCmd = new SqlCommand(proc, SQLConn);
SQLCmd.CommandType = System.Data.CommandType.StoredProcedure;
}
public void AddSQLCmdParameter(String pName, System.Data.SqlDbType pType, object pVal)
{
if (SQLCmd == null)
{
throw (new System.Exception("Must use SetSQLCommand to initialize SQLCommand object!"));
}
if (SQLConn == null)
{
OpenSQLDB();
}
try
{
SQLCmd.Parameters.Add(pName, pType).Value = pVal;
}
catch (Exception ex)
{
throw (new System.Exception(ex.Message.ToString()));
}
}
public void ClearSQLCmdParameters()
{
if (SQLCmd != null)
{
SQLCmd.Parameters.Clear();
}
}
public void DisposeSQLCmd()
{
if (SQLCmd != null)
{
SQLCmd.Dispose();
}
}
public System.Data.SqlClient.SqlDataReader SQLExecuteReader()
{
System.Data.SqlClient.SqlDataReader dr;
if (SQLCmd == null)
{
throw (new System.Exception("Must use SetSQLCommand to initialize SQLCommand object!"));
}
if (SQLConn == null)
{
OpenSQLDB();
}
try
{
dr = SQLCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw (new System.Exception(ex.Message.ToString()));
}
finally
{
SQLCmd.Dispose();
SQLCmd=null;
}
return dr;
}
}
}
然后可以像这样重用此对象:
public void ProcessCustomerData(DataTable dt)
{
//we have a valid connection to the database
if (dt.Rows.Count > 0)
{
try
{
s=new SQLClass();
}
catch (Exception e)
{
throw new System.Exception(e.Message.ToString());
}
foreach(DataRow dr in dt.Rows)
{
tw.WriteLine("Processing customer: " + dr["NAME1"].ToString());
Console.WriteLine("Processing customer: " + dr["NAME1"].ToString());
s.SetSQLCommand("insCustomer");
s.AddSQLCmdParameter("@ClientID", System.Data.SqlDbType.Int, dr["MANDT"]);
s.AddSQLCmdParameter("@CustomerID", System.Data.SqlDbType.BigInt, dr["KUNNR"]);
s.AddSQLCmdParameter("@CustomerName1", System.Data.SqlDbType.VarChar, ((string)dr["NAME1"]==String.Empty ? DBNull.Value : dr["NAME1"]));
s.AddSQLCmdParameter("@CustomerName2", System.Data.SqlDbType.VarChar, ((string)dr["NAME2"]==String.Empty ? DBNull.Value : dr["NAME2"]));
s.AddSQLCmdParameter("@Country", System.Data.SqlDbType.VarChar, ((string)dr["LAND1"]==String.Empty ? DBNull.Value : dr["LAND1"]));
s.AddSQLCmdParameter("@Region", System.Data.SqlDbType.VarChar, ((string)dr["REGIO"]==String.Empty ? DBNull.Value : dr["REGIO"]));
s.AddSQLCmdParameter("@City", System.Data.SqlDbType.VarChar, ((string)dr["ORT01"]==String.Empty ? DBNull.Value : dr["ORT01"]));
s.AddSQLCmdParameter("@ZipCode", System.Data.SqlDbType.VarChar, ((string)dr["PSTLZ"]==String.Empty ? DBNull.Value : dr["PSTLZ"]));
s.AddSQLCmdParameter("@Address", System.Data.SqlDbType.VarChar, ((string)dr["STRAS"]==String.Empty ? DBNull.Value : dr["STRAS"]));
s.AddSQLCmdParameter("@Telephone", System.Data.SqlDbType.VarChar, ((string)dr["TELF1"]==String.Empty ? DBNull.Value : dr["TELF1"]));
s.AddSQLCmdParameter("@Fax", System.Data.SqlDbType.VarChar, ((string)dr["TELFX"]==String.Empty ? DBNull.Value : dr["TELFX"]));
s.AddSQLCmdParameter("@DateAdded", System.Data.SqlDbType.DateTime, System.DateTime.Today);
s.AddSQLCmdParameter("@DateModified", System.Data.SqlDbType.DateTime, System.DateTime.Today);
s.AddSQLCmdParameter("@AddedBy", System.Data.SqlDbType.VarChar, DBNull.Value);
s.AddSQLCmdParameter("@ModifiedBy", System.Data.SqlDbType.VarChar, DBNull.Value);
s.SQLExecuteNonQuery();
Console.WriteLine("Processed customer: " + dr["NAME1"].ToString());
tw.WriteLine("Processed customer: " + dr["NAME1"].ToString());
}
s.CloseSQLDB();
s.Dispose();
}
}
OpenConnection
中的Catch:End Try
是否屏蔽了一个异常并隐藏了您从未连接到数据库,因此从未执行SQL的事实
此外,您似乎从未关闭过连接。在
OpenConnection
中的Catch:End Try
是否屏蔽了异常并隐藏了您从未连接到数据库,因此从未执行SQL的事实
此外,您似乎从未关闭过连接。我猜SQL语句中存在阻止插入发生的条件 考虑到没有抛出异常,我敢打赌语句实际上正在被处理,但WHERE子句阻止插入任何行
我将对此进行研究。我的猜测是,SQL语句中存在阻止插入发生的条件 考虑到没有抛出异常,我敢打赌语句实际上正在被处理,但WHERE子句阻止插入任何行 我会调查的。你的新()函数在你的类中哪里?您能从类中的OpenConnection()函数内部调试m_sConnectionString吗?您是否从该函数获得了真实的返回值?如果oSQL.OpenConnection失败,您没有'Else'子句,因此您不知道是否正在连接。您的新()函数在类中的位置?您能从类中的OpenConnection()函数内部调试m_sConnectionString吗?您是否从该函数获得了真实的返回值?在oSQL.OpenConnection失败的情况下,您没有“Else”子句,因此您不知道是否正在连接 VisualStudio将创建 数据库文件,将其添加到 项目,并修改连接 所以它现在指向数据库 在你的项目中 VisualStudio将创建 数据库文件,将其添加到 项目,并修改连接 所以它现在指向数据库 在你的项目中
请告诉我您没有将用户参数直接连接到strSQL变量中?那太糟糕了。改为使用查询参数。Catch:End Try-不要吞咽EXCEPTIONS@Joel:它只是一个硬编码的SQL语句,没有用户参数@WefWefWe:Fixed请告诉我您没有将用户参数直接连接到strSQL变量中?那太糟糕了。改为使用查询参数。Catch:End Try-不要吞咽EXCEPTIONS@Joel:它只是一个硬编码的SQL语句,没有用户参数@wefwfwe:fixemd_objConnection是在我添加到文章中的“OpenConnection”函数中设置的。我想说的是,使用SQL事件探查器来确定插入是否正在进入数据库。您可以只为DB名称或DBID筛选探查器。我在文章中添加了“OpenConnection”函数,其中设置了DBID.m_objConnection。我建议使用SQL探查器来确定插入是否正在进入您的DB。您可以仅为该DB名称或DBID筛选探查器。SQL语句中没有任何条件。是一个简单的Insert语句。SQL语句上没有任何条件。是一个简单的Insert语句。我想这至少会导致Try..Catch看到的异常。很抱歉今天是星期五,咖啡没有用。OpenConnection()正在返回True,而connectionString正是我所期望的(是正确的)。无论如何,谢谢。我想这至少会引起一个Try..Catch会看到的异常。很抱歉今天是星期五,咖啡没有用。OpenConnection()正在返回True,而connectionString正是我所期望的(是正确的)。无论如何谢谢你