【问题标题】:Difficulty Inserting Data from C# form into a Database难以将 C# 表单中的数据插入数据库
【发布时间】:2016-01-22 23:56:26
【问题描述】:

我刚开始使用 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);
            }
        }
    }
}

我们将不胜感激所有帮助。

【问题讨论】:

  • 请包括结果表的定义。结果表是单列吗?

标签: c# database


【解决方案1】:

您的 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();

        }
    }
}

【讨论】:

  • 谢谢,这很有意义。我现在唯一的问题是当它到达 command.ExecuteNonQuery();它给了我一个由列名或提供的值的数量与表定义不匹配引起的未处理错误。据我所知,我的 Coloumns 是“第一”“第二”等。感谢您的宝贵时间。
  • 你能从sql server 获取表定义吗?如果列名确实是第一,第二,第三......那么我需要改变我的答案。
  • 我编辑了我的答案以反映一个有六列的表格。您可能需要根据需要更改此设置或表定义。
  • SQL server中属性反映的名字有“First”“Second”等
  • 好的,代码现在可以运行了,但是我的服务器资源管理器中的表没有反映任何新增内容。我应该寻找其他位置来确认它正在将值插入数据库吗?
【解决方案2】:

这个问题看起来像以前被问过和回答过的 - 看看这里 - How to insert data into SQL Server

edit - 我的第一印象是错误的,我看不到您在哪里对数据库执行查询。它已经有一段时间没有使用 orm 编写 ado 代码手册了,如果我错了,请原谅我。

【讨论】:

    猜你喜欢
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多