如何使用C#将插入查询写入windows表单应用程序 [英] How to write insert query into windows form application using C#

查看:70
本文介绍了如何使用C#将插入查询写入windows表单应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button2_Click(object sender, EventArgs e)
       {
           string con = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Ravi\Documents\OnlineBanking.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
           SqlConnection conn = new SqlConnection(con);
           conn.Open();
           SqlCommand chk = new SqlCommand("Insert into Registration(Name)values(@Name)",conn);

           chk.Parameters.AddWithValue("@Name", textBox1.Text);
           chk.ExecuteNonQuery();

           MessageBox.Show("Account created successfully!!!");
       }





我的尝试:



我无法将值插入数据库.. plz纠正我的错误。



What I have tried:

I am unable to insert value into database.. plz rectify my error.

推荐答案

您好,试试这个代码



Hello, Try this code

private void button2_Click(object sender, EventArgs e)
       {
           string con = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Ravi\Documents\OnlineBanking.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
           SqlConnection conn = new SqlConnection(con);
           SqlCommand chk = new SqlCommand("Insert into Registration(Name)values(@Name)",conn);
	chk .CommandType = CommandType.Text;
    chk .CommandText = sqlQuery;
    chk .Parameters.Add("@Name", textBox1.Text);
     chk .Open();
    chk .ExecuteNonQuery();
    chk .Close();
 
           MessageBox.Show("Account created successfully!!!");
       }


这篇关于如何使用C#将插入查询写入windows表单应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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