简体   繁体   English

将数据插入SQL Server数据库-Winforms

[英]Insert Data into SQL Server database - Winforms

I am trying to insert the data once user clicks on button "druk". 我试图在用户单击按钮“ druk”后插入数据。 The connection string does not seem to be set properly as the debug stop at this point and does not go further. 由于调试停止,此时连接字符串似乎设置不正确,并且无法继续进行。 I have the Data Connection set and connected. 我已设置并连接了数据连接。 I have removed and replaced username from connection string due to security reasons. 由于安全原因,我已经从连接字符串中删除并替换了用户名。

  • Server: s59.hekko.net.pl 伺服器: s59.hekko.net.pl
  • Database name: truex2_kuba 数据库名称: truex2_kuba
  • Database table: barcode 数据库表: barcode

Code: 码:

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

Two things: 两件事情:

  • Initial Catalog should be set to your database name, not a username. Initial Catalog应设置为您的数据库名称,而不是用户名。 Since you have set Integrated Security=true there is no need to pass in a username or password in your connection string - it will use the user account that is running in the context of your app. 由于已将Integrated Security=true设置Integrated Security=true ,因此无需在连接字符串中传递用户名或密码-它会使用在应用程序上下文中运行的用户帐户。
  • The Data Source property should be Data Source 数据源属性应为Data Source

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

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

    }
}

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

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