在数据库中插入数据的最佳方法是什么? [英] What is the best method to insert Data in database?

查看:127
本文介绍了在数据库中插入数据的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Command对象从数据库中插入,更新和删除记录的最佳方法是什么?并且请定义此代码.

What is the best method to insert,update and delete record from database by Command object? And Please define code of this.

推荐答案

在问这样的问题之前,我建议您先阅读ADO.NET上的一些书,然后尝试一些基本的例子被问到的问题非常简单,这是学生在进行点网课程时想到的问题,我也教点网,我给了学生一个例子,告诉他们其余的工作.我对您的建议是尝试阅读一些书籍,对您进行自我训练,然后,如果您没有得到答案,那么请向我们展示您的尝试,我们一定会通过纠正您在代码简单规则中所犯的错误来为您提供帮助我的朋友,如果你不训练自己,你就不能成为一个好的程序员...

Hers ur代码,用于将数据添加到数据库中

Before asking questions like this one i would suggest u to read some books on ADO.NET and try some basic examples what u have asked is very simple and easy query that is thought to the students while doing their dot net course, i too teach dot net i give students an example and tell them to do the rest. My suggesstion for u would be to try reading some books, train ur self hard and then if ur not getting the anser then show us what u tried and we will surely help u on that by correcting the mistakes that u made in ur code simple rule my friend if u dont train urself u can''t be a good programmer...

Hers ur code for adding the data into the database

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; // This namespace will add,del,update data 

namespace WindowsApplication10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //create object of sqlconnection class
            using (SqlConnection con = new SqlConnection())
            {
//Define the connection string,Initial Catalog is ur Database name,
//Data Source indicate that db is in ur local machine
                con.ConnectionString = "Data Source=.;Initial Catalog=master;Integrated Security=True";
//open the connection
                con.Open();
//Simple SQL Query 2insert data into db here dummy is the name of table 
// and myval is just a variable that i have created...
                String Query = "insert into dummy values(@myval)";
//Creating Object for sqlcommand class which takes 2 parameter
                SqlCommand cmd = new SqlCommand(Query,con);
// Insert data into db by a parameter i.e. on which column the data will
//go from which component, myval is just a variable that i have used
                cmd.Parameters.AddWithValue("@myval", textBox1.Text);
//Execute the query
                cmd.ExecuteNonQuery();
                if (MessageBox.Show("Data Inserted") == DialogResult.OK)
                {
// on the hit of ok button i am clearing my textbox here
                    textBox1.Clear();
                }
//After ur data is inserted close the connection...
                con.Close();
// Calling the Dispose method once u close the connection... will
// close the connection and make the reference of the connection
//object to null...
                con.Dispose();
            }

        }
    }
}



上面的代码是将数据添加到数据库中,现在您要做的就是写查询以进行更新和删除,其余的其他内容是相同的
一旦找到有用的答案,就对我的答案进行评分...

谢谢&问候
基数:rose:



the above code is to add data into database now all u have to do is write queries for update and delete rest other stuff is the same
Do rate my answer once u find it useful...

Thanks & Regards
Radix :rose:


您这样做的最佳方法是编写一个适当的数据层,该数据层调用存储过程,并在表示层需要时调用此方法,并传入相关数据
Your best method to do this, is to write a proper data layer, which calls stored procedures, and is called as needed by your presentation layer, passing in the relevant data.


将数据插入数据库的最佳方法是传递Sqlparameter
The best way to insert data to database is by passing Sqlparameter


这篇关于在数据库中插入数据的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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