本文介绍了无法增加数据库的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在每次点击按钮时增加数据库值。
我尝试过的事情:
使用System;
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Data.SqlClient;
命名空间online_test
{
公共部分类Form1:表格
{
int incre = 0;
public Form1()
{
InitializeComponent();
radioButton1.TabStop = false;
radioButton2.TabStop = false;
radioButton3.TabStop = false;
radioButton4.TabStop = false;
// qn1();
}
private void Form1_Load(object sender,EventArgs e)
{
// string source_1 = @DataSource =(LocalDB)\ MSSQLLocalDB; + @AttachDbFilename = C:\Users\NASH\Documents\Visual Studio 2015 \Projects\C#\ online test \ online test \Database1.mdf; +综合安全=真; +database =< test.mdf';;
string source_1 = @Data Source =(LocalDB)\ MSSQLLocalDB; + @AttachDbFilename = C:\Users\NASH\Documents\Visual Studio 2015 \Projects\ ## onon test \ online test \ test.mdf; +Integrated Security = True;
SqlConnection Con_1 =新的SqlConnection(source_1);
{
Con_1.Open();
MessageBox.Show(连接到db);
String sqlSelectQuery = @SELECT * FROM [Table];
SqlCommand cmd = new SqlCommand(sqlSelectQuery,Con_1);
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
// incre +;
{
label2.Text =(dr [Question]。ToString());
textBox1.Text =(dr [sl]。ToString());
radioButton1.Text =(dr [Option A]。ToString());
radioButton2.Text =(dr [Option B]。ToString());
radioButton3.Text =(dr [Option C]。ToString());
radioButton4.Text =(dr [Option D]。ToString());
}
incre = Convert.ToInt32(textBox1.Text);
//递增++;
// @SELECT * FROM [Table] WHERE sl =,增量;
}
Con_1.Close();
}
}
// button1_click是下一个按钮
private void button1_Click(object sender,EventArgs e)
{
incre ++;
String sqlSelectQuery = @SELECT * FROM [Table] WHERE sl = incre;
if(dr.Read())
{
// incre +;
{
label2.Text =(dr [Question]。ToString());
textBox1.Text =(dr [sl]。ToString());
radioButton1.Text =(dr [Option A]。ToString());
radioButton2.Text =(dr [Option B]。ToString());
radioButton3.Text =(dr [Option C]。ToString());
radioButton4.Text =(dr [Option D]。ToString());
}
}
// button3_click是上一个按钮
private void button3_Click(object sender,EventArgs e)
{
}
private void button2_Click(object sender,EventArgs e)
{
}
private void textBox1_TextChanged(对象发件人,EventArgs e)
{
}
}
}
解决方案
i want to increment the database value with every click of the button.
What I have tried:
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.Data.SqlClient; namespace online_test { public partial class Form1 : Form { int incre = 0; public Form1() { InitializeComponent(); radioButton1.TabStop = false; radioButton2.TabStop = false; radioButton3.TabStop = false; radioButton4.TabStop = false; //qn1(); } private void Form1_Load(object sender, EventArgs e) { //string source_1 = @"DataSource=(LocalDB)\MSSQLLocalDB;" + @"AttachDbFilename=C: \Users\NASH\Documents\Visual Studio 2015\Projects\C#\online test\online test\Database1.mdf;" + "Integrated Security=True;" + "database=<test.mdf';"; string source_1 = @"Data Source = (LocalDB)\MSSQLLocalDB;" + @"AttachDbFilename=C:\Users\NASH\Documents\Visual Studio 2015\Projects\C#\online test\online test\test.mdf;" + "Integrated Security = True"; SqlConnection Con_1 = new SqlConnection(source_1); { Con_1.Open(); MessageBox.Show("connected to db"); String sqlSelectQuery = @"SELECT * FROM[Table]"; SqlCommand cmd = new SqlCommand(sqlSelectQuery, Con_1); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { //incre++; { label2.Text = (dr["Question"].ToString()); textBox1.Text = (dr["sl"].ToString()); radioButton1.Text = (dr["Option A"].ToString()); radioButton2.Text = (dr["Option B"].ToString()); radioButton3.Text = (dr["Option C"].ToString()); radioButton4.Text = (dr["Option D"].ToString()); } incre = Convert.ToInt32(textBox1.Text); // incre++; // @"SELECT * FROM[Table]WHERE sl=",incre; } Con_1.Close(); } } //button1_click is next button private void button1_Click(object sender, EventArgs e) { incre++; String sqlSelectQuery = @"SELECT * FROM[Table] WHERE sl=incre"; if (dr.Read()) { //incre++; { label2.Text = (dr["Question"].ToString()); textBox1.Text = (dr["sl"].ToString()); radioButton1.Text = (dr["Option A"].ToString()); radioButton2.Text = (dr["Option B"].ToString()); radioButton3.Text = (dr["Option C"].ToString()); radioButton4.Text = (dr["Option D"].ToString()); } } // button3_click is previous button private void button3_Click(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { } private void textBox1_TextChanged(object sender, EventArgs e) { } } }
解决方案
这篇关于无法增加数据库的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!