繁体   English   中英

使用C#将数据添加到Microsoft的SQL Server文件中

[英]Adding data to a Microsoft's SQL Server file using c#

我已经在项目中添加了Microsoft的SQL Server文件,并且正在运行SqlCommand将数据插入文件中。 我正在using System.Data.SqlClient; 以下代码是我向文件中添加数据的方式。 程序运行完毕后,我转到项目中的数据资源管理器,并要求显示HistQuote表数据, 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();
}

整个用户实例和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)出色的博客文章不良习惯:使用AttachDbFileName获取更多背景信息。

这样的事情可能行得通吗?

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();
        }
    }
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM