【问题标题】:Adding data to a Microsoft's SQL Server file using c#使用 c# 将数据添加到 Microsoft 的 SQL Server 文件
【发布时间】:2015-04-04 15:23:57
【问题描述】:

我已将 Microsoft 的 SQL Server 文件添加到我的项目中,并且正在运行 SqlCommand 将我的数据插入到文件中。我是using System.Data.SqlClient;。以下代码是我如何将数据添加到我的文件中。在我的程序完成运行后,我转到项目中的数据资源管理器并要求显示HistQuote 的表数据,但没有任何显示。谁能建议我如何验证我的 INSERT 语句是否有效。

using (SqlConnection connection = new SqlConnection(Settings.Default.StorageConnectionString))
{
    connection.Open();
    for (int intCurrentQuote = 0; intCurrentQuote < this.clbStockSelect.CheckedItems.Count; ++intCurrentQuote)
    {
        for (int intCurrentDate = 0; intCurrentDate < Quotes[intCurrentQuote].HistStockDate.Count; ++intCurrentDate)
        {
            string strInsert = "INSERT INTO [HistQuote] ";
            string strColumns = "(Symbol, [Date], [Open], High, Low, Volume, Adj_Close, [Close]) ";
            string strValues = "VALUES (@Symbol, @Date, @Open, @High, @Low, @Volume, @Adj_Close, @Close)";

            using (SqlCommand sqlCommand = new SqlCommand(strInsert + strColumns + strValues, connection))
            {
            sqlCommand.Parameters.Clear();
            sqlCommand.Parameters.Add(new SqlParameter("@Symbol", SqlDbType.NChar));
            sqlCommand.Parameters.Add(new SqlParameter("@Date", SqlDbType.DateTime));
            sqlCommand.Parameters.Add(new SqlParameter("@Open", SqlDbType.Real));
            sqlCommand.Parameters.Add(new SqlParameter("@High", SqlDbType.Real));
            sqlCommand.Parameters.Add(new SqlParameter("@Low", SqlDbType.Real));
            sqlCommand.Parameters.Add(new SqlParameter("@Close", SqlDbType.Real));
            sqlCommand.Parameters.Add(new SqlParameter("@Volume", SqlDbType.Real));
            sqlCommand.Parameters.Add(new SqlParameter("@Adj_Close", SqlDbType.Real));
            sqlCommand.Parameters["@Symbol"].Size = 10;

            sqlCommand.Prepare();

            sqlCommand.Parameters["@Symbol"].Value = this.Quotes[intCurrentQuote].HistSymbol;
            sqlCommand.Parameters["@Date"].Value = this.Quotes[intCurrentQuote].HistStockDate[intCurrentDate];
            sqlCommand.Parameters["@Open"].Value = this.Quotes[intCurrentQuote].HistOpen[intCurrentDate];
            sqlCommand.Parameters["@High"].Value = this.Quotes[intCurrentQuote].HistHigh[intCurrentDate];
            sqlCommand.Parameters["@Low"].Value = this.Quotes[intCurrentQuote].HistLow[intCurrentDate];
            sqlCommand.Parameters["@Close"].Value = this.Quotes[intCurrentQuote].HistClose[intCurrentDate];
            sqlCommand.Parameters["@Volume"].Value = this.Quotes[intCurrentQuote].HistVolume[intCurrentDate];
            sqlCommand.Parameters["@Adj_Close"].Value = this.Quotes[intCurrentQuote].HistAdjClose[intCurrentDate];
            sqlCommand.ExecuteNonQuery();
            sqlCommand.Parameters.Clear();
            }
        }
    }
    connection.Close();
}

【问题讨论】:

  • 你能告诉我们你的连接字符串吗?
  • 我的连接字符串是Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Storage.mdf;Integrated Security=True;User Instance=True

标签: c# sql-server insert-statement


【解决方案1】:

整个 User Instance 和 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中创建你的数据库,给它一个逻辑名称(例如Storage

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

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

    其他一切都一模一样和以前一样......

有关更多背景信息,另请参阅 Aaron Bertrand 的优秀博文 Bad habits to kick: using AttachDbFileName

【讨论】:

  • 我以为是这样的。对于您提出的解决方案安装 SQL Server Express 我已经安装了 SQL Server 2012。我一直在网上寻找 安装 SQL Server Management Studio Express 但我必须注意什么版本我在用。我找到了以下链接SSMS Express,但它是 SQL Server 2005 的管理工具,这是我的第一个区别。你会推荐安装SQL Server Express
  • @Andraro:如果您安装了 SQL Server 2012 的完整版本(Express 除外),那么应该包括 Management Studio 的完整版本 - 在这种情况下无需安装 Express 和 Mgmt Studio Express ...
  • 感谢您的澄清。是否可以让我知道您如何确定您在答案中提供的连接字符串的Data Surce
  • 为了将来参考,您可以使用以下链接到Find the connection string of the DB
【解决方案2】:

这样的事情可能有用吗?

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dataread
{
    class Program
{
    static void Main(string[] args)
    {
        using (SqlConnection connection = new SqlConnection(Settings.Default.StorageConnectionString))
        {
            connection.Open();
            string strCmd = "Select * from [HistQuote]";

            using (SqlCommand sqlCommand = new SqlCommand(strCmd, connection))
            {
                var rdr = new SqlDataReader();
                rdr = sqlCommand.ExecuteReader();
                while(rdr.Read())
                {
                    Console.WriteLine(rdr["Symbol"].ToString() + rdr["Date"].ToString() + rdr["Open"].ToString() + rdr["High"].ToString() + rdr["Low"].ToString() + rdr["Volume"].ToString() + rdr["Adj_Close"].ToString() + rdr["Close"].ToString());
                }
            }
            connection.Close();
        }
    }
}
}

【讨论】:

  • rdr.Read() 返回 false。
猜你喜欢
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
  • 1970-01-01
相关资源
最近更新 更多