难以将数据从C#表单插入数据库

时间:2016-01-22 15:27:14

标签: c# database

我刚刚开始使用Visual Studio 2015学习C#,我的任务是创建一个将生成的数字保存到数据库中的彩票程序。我尝试了各种方法,似乎没有任何方法可以对我的表格进行任何添加。任何人都可以帮我理解我需要做什么采取已经生成并转换为字符串/文本框然后将该值插入我的表中的整数。

下面是我当前的代码,按钮2是我试图用来保存文本框中数据的按钮。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        //Database details
        string connectionString;
        SqlConnection connection;
        public Form1()
        {
            InitializeComponent();
            connectionString = ConfigurationManager.ConnectionStrings["WindowsFormsApplication2.Properties.Settings.LottoConnectionString"].ConnectionString;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Random rnd = new Random();
            int[] slot = new int[6];
            int counter = 0;

            for (int i = 0; i < slot.Length; i++)
            {
                slot[i] = rnd.Next(0, 100);
            }

            //Converting generated ints to Strings for display
            textBox1.Text = (slot[0].ToString());
            textBox2.Text = (slot[1].ToString());
            textBox3.Text = (slot[2].ToString());
            textBox4.Text = (slot[3].ToString());
            textBox5.Text = (slot[4].ToString());
            textBox6.Text = (slot[5].ToString());

            //Incrementing Counter checks matches
            if (numericUpDown1.Value == slot[0])
            {
                counter += 1;
            }
            if (numericUpDown2.Value == slot[1])
            {
                counter += 1;
            }
            if (numericUpDown3.Value == slot[2])
            {
                counter += 1;
            }
            if (numericUpDown4.Value == slot[3])
            {
                counter += 1;
            }
            if (numericUpDown5.Value == slot[4])
            {
                counter += 1;
            }
            if (numericUpDown6.Value == slot[5])
            {
                counter += 1;
            }

            //display total matches
            textBox7.Text = ("You got" + counter + "/6 matches!");

           LottoDataSetTableAdapters.ResultsTableAdapter resultsTableAdapter =
            new LottoDataSetTableAdapters.ResultsTableAdapter();

            resultsTableAdapter.Insert((slot[0].ToString()), (slot[1].ToString()), (slot[2].ToString()), (slot[3].ToString()), (slot[4].ToString()), (slot[5].ToString()));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Adding Data to Database
            string query = "INSERT INTO Results VALUES (@First)";
            using (connection = new SqlConnection(connectionString))
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                connection.Open();
                command.Parameters.AddWithValue("@First", textBox1.Text);
                command.Parameters.AddWithValue("@Second", textBox2.Text);
                command.Parameters.AddWithValue("@Third", textBox3.Text);
                command.Parameters.AddWithValue("@Fourth", textBox4.Text);
                command.Parameters.AddWithValue("@Fifth", textBox5.Text);
                command.Parameters.AddWithValue("@Sixth", textBox6.Text);
            }
        }
    }
}

非常感谢所有帮助。

2 个答案:

答案 0 :(得分:0)

您的INSERT语句缺少VALUES部分中的其他参数。您还需要执行该命令,并且缺少使用连接的括号。

private void button2_Click(object sender, EventArgs e)
{
    // Adding Data to Database
    string query = "INSERT INTO Results (First, Second, Third, Fourth, Fifth, Sixth) VALUES (@First, @Second, @Third, @Fourth, @Fifth, @Sixth)";
    using (var connection = new SqlConnection(connectionString))
    {
        using (SqlCommand command = new SqlCommand(query, connection))
        {
            connection.Open();
            command.Parameters.AddWithValue("@First", textBox1.Text);
            command.Parameters.AddWithValue("@Second", textBox2.Text);
            command.Parameters.AddWithValue("@Third", textBox3.Text);
            command.Parameters.AddWithValue("@Fourth", textBox4.Text);
            command.Parameters.AddWithValue("@Fifth", textBox5.Text);
            command.Parameters.AddWithValue("@Sixth", textBox6.Text);

            command.ExecuteNonQuery();

        }
    }
}

答案 1 :(得分:-1)

看起来这个问题之前已经被问过并回答了 - 看看这里 - How to insert data into SQL Server

编辑 - 我的第一印象是错误的,我无法看到您在对数据库执行查询的位置。它已经有一段时间,因为它没有使用orm编写了ado代码手册,所以如果我错了就原谅我。