是'字段'但是像'类型'一样使用 [英] Is a 'field' but is used like a 'type'

查看:66
本文介绍了是'字段'但是像'类型'一样使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;
使用 System.Configuration;
使用 System.Data.SqlClient;

命名空间 Emp_Table
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

SqlConnection con = new SqlConnection(Helper.cs);
SqlCommand cmd = new SqlCommand();

private void btnInsert_Click( object sender,EventArgs e)
{
int Id = Convert.ToInt32(txtId.Text);
string Name = txtName.Text;
decimal Sal = Convert.ToDecimal(txtSal.Text);
con.Command Text = 插入Emp(EmpId,EmpName,Salary)值( + Id + ,' + Name + ', + Sal + ;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show( 插入表)的值;
}

public string id {获得; set ; }

private void lblSal_Click(对象发​​件人,EventArgs e)
{

}
}
}





 con.Command Text =  插入Emp( EmpId,EmpName,Salary)值( + Id +  ,' +名称+  ', + Sal +  ; 



得到错误

解决方案

< blockquote>

 con.Command Text =  插入Emp(EmpId,EmpName,Salary)值( + Id +  ,' + Name +  ', + Sal +  ; 



上面的单词 Text 是错误;删除它。



您还需要查看代码的其余部分。您没有为包含无效字符或值的任何文本框做出任何规定。进行一些正确的错误检查,或者最终导致应用程序崩溃。


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.Configuration;
using System.Data.SqlClient;

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

        SqlConnection con = new SqlConnection(Helper.cs);
        SqlCommand cmd = new SqlCommand();
            
        private void btnInsert_Click(object sender, EventArgs e)
        {
            int Id = Convert.ToInt32(txtId.Text);
            string Name = txtName.Text;
            decimal Sal = Convert.ToDecimal(txtSal.Text);
            con.Command Text = "Insert into Emp(EmpId, EmpName, Salary) values (" + Id + ",'" + Name + "'," + Sal + ")";
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Values inserted into the Table");
        }

        public string id { get; set; }

        private void lblSal_Click(object sender, EventArgs e)
        {

        }
    }
}



con.Command Text = "Insert into Emp(EmpId, EmpName, Salary) values (" + Id + ",'" + Name + "'," + Sal + ")";


getting error

解决方案

con.Command Text = "Insert into Emp(EmpId, EmpName, Salary) values (" + Id + ",'" + Name + "'," + Sal + ")";


The word Text in the above is the error; remove it.

You also need to look at the remainder of your code. You have made no provision for any of the text boxes containing invalid characters or values. Put some proper error checks in or you will end up with your application crashing.


这篇关于是'字段'但是像'类型'一样使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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