亲爱的,在qurey之下,我们在C#中将数据插入SQL db..i我无法获得。任何人都可以帮助我们恢复。 [英] Dear all, below qurey we worte in C# to insert the data to SQL db..i I could not get secceed. Can any one help us to resove.

查看:65
本文介绍了亲爱的,在qurey之下,我们在C#中将数据插入SQL db..i我无法获得。任何人都可以帮助我们恢复。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Econtact
{
    class Contactclass
    {
        //public int  { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        

        static string myconnection = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;

        public DataTable Select()
        {
            SqlConnection conn = new SqlConnection(myconnection);
            DataTable dt = new DataTable();
            try
            {
                string sql = "SELECT * FROM tbl_contact";
              
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                conn.Open();
                adapter.Fill(dt);
            }
            catch (Exception)
            {

            }
            finally
            {
                conn.Close();
            }
            return dt;

        }
        public bool Insert(Contactclass c)
        {
            bool issuccess = false;
            SqlConnection conn = new SqlConnection(myconnection);
            try
            {

                string sql = "INSERT INTO tbl_contact('FirstName', 'LastName', 'Address') VALUES('@FirstName', '@LastName', '@Address')";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@FirstName", c.FirstName);
                cmd.Parameters.AddWithValue("@LastName", c.LastName);
                cmd.Parameters.AddWithValue("@Address", c.Address);

                conn.Open();
                int rows = cmd.ExecuteNonQuery();

                if (rows > 0)
                {
                    issuccess = false;
                }
                else
                {
                    issuccess = true;
                }
              }


            catch (Exception)
            {

            }
            finally
            {
                conn.Close();
            }
                return issuccess;
            
        }
        
    }
}





什么我试过了:



亲爱的所有人,在Qurey下面,我们用C#编写了将数据插入SQL db ..我无法成功。任何人都可以帮我们解决。(使用VB,Windows窗体)



What I have tried:

Dear All, below Qurey we wrote in C# to insert the data to SQL db.. i could not get succeed. can any one help us to resolve.(Using VB , windows form)

推荐答案

将您的查询字符串更改为:



Change your query string to this:

string sql = "INSERT INTO tbl_contact (FirstName, LastName, Address) VALUES (@FirstName, @LastName, @Address)";


引用...它们在SQL中标记字符串。所以,当你把参数名称放在一边时,它们会被视为文字字符串。

试试这个:

Quotes ... they mark strings in SQL. So when you put then round your parameter names, they get taken as literal strings.
Try this:
string sql = "INSERT INTO tbl_contact(FirstName, LastName, Address) VALUES(@FirstName, @LastName, @Address)";


这篇关于亲爱的,在qurey之下,我们在C#中将数据插入SQL db..i我无法获得。任何人都可以帮助我们恢复。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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