【问题标题】:Insert Data into SQL Server database - Winforms将数据插入 SQL Server 数据库 - Winforms
【发布时间】:2016-09-02 00:08:57
【问题描述】:

一旦用户单击“druk”按钮,我就会尝试插入数据。连接字符串似乎没有正确设置,因为此时调试停止并且没有进一步。我已经设置并连接了数据连接。出于安全原因,我已从连接字符串中删除并替换了用户名。

  • 服务器:s59.hekko.net.pl
  • 数据库名称:truex2_kuba
  • 数据库表:barcode

代码:

private void druk_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "DataSource=s59.hekko.net.pl; Initial Catalog=username; Integrated security=true";
    con.Open();

    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "insert into [barcode]values(@class, @tree, @type, @amount, @length, @width, @square)";

    cmd.Parameters.AddWithValue("@class", klasa.Text);
    cmd.Parameters.AddWithValue("@tree", gatunek.Text);
    cmd.Parameters.AddWithValue("@type", rodzaj.Text);
    cmd.Parameters.AddWithValue("@amount", amount.Text);
    cmd.Parameters.AddWithValue("@length", length.Text);
    cmd.Parameters.AddWithValue("@width", width.Text);
    cmd.Parameters.AddWithValue("@square", textBox1.Text);

    int a = cmd.ExecuteNonQuery();

    if (a > 0)
    {
        MessageBox.Show("Zapisane do raportu");
    }
}

【问题讨论】:

  • 初始目录应设置为 truex2_kuba 而用户名和密码需要设置其密钥,集成安全性似乎不是在远程数据库上使用的正确密钥
  • 您有什么错误信息要分享吗?
  • 它只说:抛出异常:System.Data.dll 中的“System.ArgumentException”附加信息:不支持关键字:“数据源”。
  • @Steve,我应该使用什么来代替集成安全性?我应该在哪里输入数据库密码?谢谢
  • 我不确定您的配置,但如果该服务器未连接到您自己的网络或以某种方式链接到您的 Active Directory,那么我认为集成安全性无法工作。相反,如果此数据库托管在某处并且您正确设置了所有防火墙规则,那么您可以添加托管服务提供商提供给您的用户名和密码。有关连接详细信息,请查看ConnectionStrings.com

标签: c# sql-server winforms visual-studio


【解决方案1】:

两件事:

  • Initial Catalog 应该设置为您的数据库名称,而不是用户名。由于您已设置Integrated Security=true,因此无需在连接字符串中传递用户名或密码 - 它将使用在您的应用程序上下文中运行的用户帐户。
  • 数据源属性应为Data Source

Data Source=s59.hekko.net.pl; Initial Catalog= truex2_kuba; Integrated security=true

【讨论】:

    【解决方案2】:
    string connectionString = GetConnectionString();
    static private string GetConnectionString()
    {
        return "Data Source = s59.hekko.net.pl; Initial Catalog = truex2_kuba; Integrated security=true";
    }
    
    private void druk_Click(object sender, EventArgs e)
    {
        string queryString = "INSERT INTO [dbo].[barcode] ([ColumnNameForClassinbarcodeTable],[ColumnNameForTreeinbarcodeTable],[ColumnNameForTypeinbarcodeTable],[ColumnNameForAmountinbarcodeTable],[ColumnNameForLengthinbarcodeTable],[ColumnNameForWidthinbarcodeTable],[ColumnNameForSquareinbarcodeTable]) VALUES (@class, @tree, @type, @amount, @length, @width, @square)";
        using (SqlConnection sqlConnection = new SqlConnection(connectionString))
        using (SqlCommand sqlCommand = new SqlCommand(queryString, sqlConnection))
        {
            try
            {
                //This example assumes all the columns are varchar(500) in your database table design, you may
                //likewise modify these to SqlDbType.Float, SqlDbType.DateTime etc. based on your design
    
                sqlCommand.Parameters.Add("@class", SqlDbType.VarChar, 500).Value = klasa.Text;
                sqlCommand.Parameters.Add("@tree", SqlDbType.VarChar, 500).Value = gatunek.Text;
                sqlCommand.Parameters.Add("@type", SqlDbType.VarChar, 500).Value = rodzaj.Text;
                sqlCommand.Parameters.Add("@amount", SqlDbType.VarChar, 500).Value = amount.Text;
                sqlCommand.Parameters.Add("@length", SqlDbType.VarChar, 500).Value = length.Text;
                sqlCommand.Parameters.Add("@width", SqlDbType.VarChar, 500).Value = width.Text;
                sqlCommand.Parameters.Add("@square", SqlDbType.VarChar, 500).Value = length.Text;
    
                sqlCommand.CommandType = CommandType.Text;
                sqlConnection.Open();
                int i = sqlCommand.ExecuteNonQuery();
                sqlConnection.Close();
    
                if (i != 0)
                {
                    MessageBox.Show("Successful Insert.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
    
                else
                    MessageBox.Show("Error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    
        }
    }
    

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 2019-11-22
    • 2017-03-17
    • 2012-01-31
    相关资源
    最近更新 更多