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

时间:2016-05-06 20:01:02

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

我试图在用户点击按钮" 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");
    }
}

2 个答案:

答案 0 :(得分:0)

两件事:

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

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

答案 1 :(得分:0)

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

    }
}