很难将C#form中的数据插入数据库
我刚刚开始使用Visual Studio 2015学习C#,我的任务是创建一个彩票程序,将生成的数字保存到数据库中。我尝试过各种各样的方法,但似乎都没有给我的表添加任何内容。有人能帮我理解我需要做什么吗?取一个已经生成并转换成字符串/文本框的整数,然后将该值插入我的表中 下面是我当前的代码,按钮2是我试图用来保存文本框中数据的按钮很难将C#form中的数据插入数据库,c#,database,C#,Database,我刚刚开始使用Visual Studio 2015学习C#,我的任务是创建一个彩票程序,将生成的数字保存到数据库中。我尝试过各种各样的方法,但似乎都没有给我的表添加任何内容。有人能帮我理解我需要做什么吗?取一个已经生成并转换成字符串/文本框的整数,然后将该值插入我的表中 下面是我当前的代码,按钮2是我试图用来保存文本框中数据的按钮 using System; using System.Collections.Generic; using System.ComponentModel; using
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);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用系统配置;
使用System.Data.SqlClient;
命名空间Windows窗体应用程序2
{
公共部分类Form1:Form
{
//数据库详细信息
字符串连接字符串;
SqlConnection连接;
公共表格1()
{
初始化组件();
connectionString=ConfigurationManager.connectionString[“WindowsFormsApplication2.Properties.Settings.LottoConnectionString”]。connectionString;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
随机rnd=新随机();
int[]插槽=新int[6];
int计数器=0;
for(int i=0;i
我们将非常感谢您的帮助。这个问题看起来像是以前问过并回答过的-请看这里-
编辑-我的第一印象是错误的,我看不出您在哪里对db执行查询。它已经有一段时间没有使用orm编写ado代码手册了,所以如果我错了,请原谅。这个问题看起来像是以前问过和回答过的-看看这里-
编辑-我的第一印象是错误的,我看不出您在哪里对db执行查询。它已经有一段时间没有使用orm编写ado代码手册了,所以如果我错了,请原谅。您的INSERT语句缺少值部分中的其他参数。您还需要执行该命令,并且在使用连接时缺少括号
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();
}
}
}
INSERT语句缺少“值”部分中的其他参数。您还需要执行该命令,并且在使用连接时缺少括号
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();
}
}
}
请包括结果表的定义。结果表是单列吗?请包括结果表的定义。结果表是一列吗?谢谢,这很有意义。我现在唯一的问题是当它到达命令.ExecuteNonQuery()时;它给我一个未处理的错误,原因是提供的列名称或值的数量与表定义不匹配。据我所知,我的冒号是“First”“Second”等。谢谢您的时间。您能从sql server获取表定义吗?如果列名真的是第一、第二、第三。。。然后我需要更改我的答案。我编辑了我的答案以反映一个有六列的表。您可能需要根据需要更改此定义或表定义。SQL server中属性中反映的名称为“第一个”“第二个”等。好的,代码现在运行,但是“我的服务器资源管理器”中的表不反映任何新添加的内容。是否有其他位置需要我确认它正在将值插入数据库?谢谢