Insert Into Statement不会抛出错误,但Data不会插入表中

时间:2014-02-24 01:37:40

标签: c# sql sql-server insert

我无法在任何地方找到这个问题的答案。我正在尝试将数据插入SQL Server表。语句似乎一切正常,命令执行时没有错误抛出,但数据没有更新到表中......

这是我的代码:

    public Boolean InsertRecordsToDB(string sColumns, String sParameters, string sValues, string sTable)
    {
        /// Split the parameter holding the values for the sql command parameters by the tab character and fill the appropriate arra\
        String[] sMyCols;
        sMyCols = sColumns.Split('\t');
        List<String> lstCols = new List<String>();
        for (int ii = 0; ii < sMyCols.Length; ii++)
        {
            lstCols.Add(sMyCols[ii]);
        }

        String[] sMyParams;
        sMyParams = sParameters.Split('\t');
        List<String> lstParams = new List<String>();
        for (int ii = 0; ii < sMyParams.Length; ii++)
        {
            lstParams.Add(sMyParams[ii]);
        }

        /// Split the values param and fill the array.  Using the parameter methodology as opposed to concatenating SQL strings prevents
        /// SQL injection
        string[] sMyValues;
        sMyValues = sValues.Split('\t');
        List<String> lstValues = new List<String>();
        for (int ii = 0; ii < sMyValues.Length; ii++)
        {
            lstValues.Add(sMyValues[ii]);
        }

        ///  Default connection string for the library project to be opened by the connection object
        String sConnString = LibraryProject.Properties.Settings.Default.db_ConnectionString;

        ///First build the Sql string based on the criteria passed in
        String sSql = "";
        sSql = "Insert Into [" + sTable + "] (";
        for (int j = 0; j < sMyCols.Length; j++)
        {
            int iParamLen = sMyCols.Length;
            int iParamMinusOne = iParamLen - 1;

            if (j.Equals(iParamMinusOne))
            {
                sSql += sMyCols[j] + ")";
            }
            else
            {
                sSql += sMyCols[j] + ",";
            }

            if (sSql.Substring(sSql.Length - 1).Equals(")"))
            {
                break;
            }
        }

        sSql += " Values(";
        for (int ii = 0; ii < sMyParams.Length; ii++)
        {
            int iParamLen = sMyParams.Length;
            int iParamLenMinusOne = iParamLen - 1;
            if (ii.Equals(iParamLenMinusOne))
            {
                sSql += sMyParams[ii].Substring(2) + ")";
            }
            else
            {
                sSql += sMyParams[ii].Substring(2) + ",";
            }

            if (sSql.Substring(sSql.Length - 1).Equals(")"))
            {
                break;
            }
        }

        ///  Create the connection object
        using (SqlConnection oConn = new SqlConnection(sConnString))
        {
            oConn.Open();
            {


                try
                {
                    /// With the connection open instantiate an Sql command object
                    SqlCommand oMyCmd = new SqlCommand(sSql, oConn);
                    oMyCmd.Connection = oConn;
                    oMyCmd.CommandType = CommandType.Text;
                    oMyCmd.CommandText = sSql;
                    int i = 0;

                    /// Assign the correct SQLDbType based on the preceding character of the parameter passed in (e.g. s_ = String/Text:  b_ = Boolean: d_ = Date)
                    foreach (String sParam in sMyParams)
                    {
                        switch (sParam.Substring(0, 2))
                        {
                            case "s_":
                                oMyCmd.Parameters.Add(sParam.Substring(2), SqlDbType.Text);

                                break;

                            case "b_":
                                oMyCmd.Parameters.Add(sParam.Substring(2), SqlDbType.Bit);
                                break;

                            case "d_":
                                oMyCmd.Parameters.Add(sParam.Substring(2), SqlDbType.Date);
                                break;

                            default:
                                break;
                        }

                        oMyCmd.Parameters[sParam.Substring(2)].Value = lstValues[i];
                        i++;
                    }
                    oMyCmd.ExecuteNonQuery();
                    return true;
                }
                catch (Exception e)
                {
                    e.ToString();
                }
                return true;
            }

        }

    }

有人请帮助我,我正在这里撞墙。

这是构建它之后的实际SQL语句:

Insert Into [tblBook] (ISBN, SERIAL_NUM, SUBJECT, TITLE, AUTHOR, PUBLISHER, GENRE)
Values(@ISBN, @SERIAL_NUM, @SUBJECT, @TITLE, @AUTHOR, @PUBLISHER, @GENRE)

语句值是从即时窗口复制的。我在oMyCmd.ExecuteNotQuery();行停止了执行。

1 个答案:

答案 0 :(得分:0)

整个用户实例和AttachDbFileName = 方法存在缺陷 - 充其量!在Visual Studio中运行应用程序时,它将复制.mdf文件(从App_Data目录到输出目录 - 通常是.\bin\debug - 应用程序运行的地方)和最有可能,您的INSERT工作正常 - 但您最后只是查看错误的.mdf文件

如果你想坚持这种方法,那么尝试在myConnection.Close()调用上设置一个断点 - 然后用SQL Server Mgmt Studio Express检查.mdf文件 - 我几乎可以肯定你的数据在那里。

我认为真正的解决方案将是

  1. 安装SQL Server Express(你已经完成了)

  2. 安装SQL Server Management Studio Express

  3. SSMS Express 中创建数据库,为其指定一个逻辑名称(例如MyDatabase

  4. 使用其逻辑数据库名称(在服务器上创建时给定)连接到它 - 并且不要乱用物理数据库文件和用户实例。在这种情况下,您的连接字符串将类似于:

    Data Source=.\\SQLEXPRESS;Database=MyDatabase;Integrated Security=True
    

    其他所有内容都完全与以前相同......