C#如何在数据库中的testbox上插入和搜索数据 [英] C# how to insert and search data on testbox within database

查看:63
本文介绍了C#如何在数据库中的testbox上插入和搜索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在构建一个允许用户输入或搜索主题名称的界面。



 SubjectName 
会计
生物学
商务
计算机科学
计算
英语
法律
数学





问题是如果我试图输入一个新的subjectName,就会出现错误

 System.AccessViolationException:'试图读或写受保护的记忆这通常表明其他内存已损坏。'在program.cs上







和另一个问题是如何将新主题名称插入数据库,我是否在btnNext按钮上实现它?文本框如何从数据库中搜索名称或插入新的主题名称



任何想法?谢谢



我尝试过:



<$ p $使用系统;
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用MySql.Data.MySqlClient;


命名空间ProjectInterface
{
公共部分类概念:表格
{

MySqlConnection con = new MySqlConnection(server = localhost; Database = databse; Port = 3306; User ID = root; Password = password123);
MySqlCommand cmd = new MySqlCommand();

public Concept()
{
InitializeComponent();
}



private void btnNextC_Click(object sender,EventArgs e)
{
con.Open();



string newcon =插入教程(TutorialName)VALUES('txtTuName.Text +');

MySqlCommand cmd = new MySqlCommand(newcon,con);
cmd.ExecuteNonQuery();



this.Hide();
Home home = new Home();
home.Show();

// next - go t oquestionnaire page
//this.Hide();
//调查问卷q =新调查问卷();
//q.Show();

}
private void btnCancelC_Click(object sender,EventArgs e)
{
//取消按钮 - 返回主页。
this.Hide();
Home home = new Home();
home.Show();
}

private void Concept_Load(object sender,EventArgs e)
{

}

private void txtSubject_TextChanged(object sender,EventArgs e)
{
con.Open();


MySqlCommand cmd = new MySqlCommand(SELECT SubjectName from databse.subject WHERE SubjectName LIKE @name,con);
cmd.Parameters.Add(new MySqlParameter(@ name,%+ txtSubject.Text +%));

MySqlDataReader dr = cmd.ExecuteReader();

AutoCompleteStringCollection subjectColl = new AutoCompleteStringCollection();

while(dr.Read()){
subjectColl.Add(dr.GetString(0));
}

txtSubject.AutoCompleteCustomSource = subjectColl;
con.Close();


}

}

解决方案

我有问题吗你想在同一个文本框上执行这两个动作。就像你试图插入子名,同时你试图搜索数据库中的主题。你必须分析哪种情况适合你的应用。

错误是因为当您尝试将数据插入数据库时​​,您已经编写了cimmand来选择数据库中的主题。我建议您仅在操作完成时执行此操作。

first你尝试从数据库中选择数据n检查匹配。



if(match)

{

//这里显示匹配的主题

}

否则如果(!匹配)

{

//这里你允许用户添加特定主题并保留任何按钮或其他东西来执行插入操作。如果你想插入这个功能就不会发生匹配会工作。显示按钮或复选框之类的控件,然后单击它然后编写插入代码。插入成功完成后,使其不可见。

}


hello, I am building a interface that allow user to input or search a subject name.

 SubjectName
Accounting
Biology
Bussiness
Computer Science
Computing
English
Law
Maths



The problem is if I tried to input a new subjectName, there will be a error showed

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.' on program.cs




and another question is how to insert the new subject name to database, do I achieve it on btnNext button? how can textbox search a name from the database or insert a new subjectname

Any ideas? Thank you

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 MySql.Data.MySqlClient;


namespace ProjectInterface
{
    public partial class Concept : Form
    {

        MySqlConnection con = new MySqlConnection("server=localhost;Database=databse;Port = 3306; User ID = root; Password = password123");
        MySqlCommand cmd = new MySqlCommand();

        public Concept()
        {
            InitializeComponent();
        }



        private void btnNextC_Click(object sender, EventArgs e)
        {
            con.Open();

      

            string newcon = "Insert into tutorials(TutorialName) VALUES ('" + txtTuName.Text + " ')";

            MySqlCommand cmd = new MySqlCommand(newcon, con);
            cmd.ExecuteNonQuery();



            this.Hide();
            Home home = new Home();
            home.Show();

            //next - go t oquestionnaire page
            //this.Hide();
            //Questionnaire q = new Questionnaire();
            //q.Show();

        }
        private void btnCancelC_Click(object sender, EventArgs e)
        {
            //cancel button - go back to home page.
            this.Hide();
            Home home = new Home();
            home.Show();
        } 

        private void Concept_Load(object sender, EventArgs e)
        {

        }

        private void txtSubject_TextChanged(object sender, EventArgs e)
        {
            con.Open();
  
            
            MySqlCommand cmd = new MySqlCommand("SELECT  SubjectName From databse.subject WHERE SubjectName LIKE @name",con);
            cmd.Parameters.Add(new MySqlParameter("@name", "%" + txtSubject.Text + "%"));

            MySqlDataReader dr = cmd.ExecuteReader();

            AutoCompleteStringCollection subjectColl = new AutoCompleteStringCollection();

            while (dr.Read()) { 
                subjectColl.Add(dr.GetString(0));
             }
            
            txtSubject.AutoCompleteCustomSource = subjectColl;
            con.Close();


        }

   }

解决方案

i have question do you want to perform both actions on same textbox .like you are trying to insert sub name and same time you are trying to search the subjects which are there in database .you have to analyse which scenario is suitable for your application.
the error is because when you try to insert data into database you have written cimmand for selecting subject which are there in database.i suggest you to perform this only when action is done.
first you try to perform selecting data from database n check for match .

if (match)
{
//here you display matched subjects
}
else if(!match)
{
//here you allow user to add the particular subject and keep any button or something to perform action for inserting .when no match occurs if you want to insert this functionality would work.display a control like button or checkbox and click on it then write code for insertion.when insertion is done successfully.make it invisible.
}


这篇关于C#如何在数据库中的testbox上插入和搜索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆