看起来很完美但却给出错误 [英] looks perfect but give error
本文介绍了看起来很完美但却给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是在C#中将插入值写入数据库
它给我的错误如下所述
i m just writing insert value to database in C#
its give me error that are describes below
1)Error ; expected line 44
2){ expected line:42
3)} expected line:37
4)Invalid expression term '}' line:44
5)Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement line:43
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.Data.SqlClient;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string name=textBox1.Text;
int age=Convert.ToInt32(textBox2.Text);
int salary=Convert.ToInt32(textBox3.Text);
try
{
string connectionstring = @"Data Source=THEONE\PARTH;Initial Catalog=testdatabase;Integrated Security=True;";
SqlConnection conn = new SqlConnection(connectionstring);
conn.Open();
SqlCommand cmd=new SqlCommand("INSERT INTO persondetail VALUES (@name,@age,@salary)",conn);
{
cmd.Parameters.AddWithValue("@name",name);
cmd.Parameters.AddWithValue("@age",age);
cmd.Parameters.AddWithValue("@salary",salary);
cmd.ExecuteNonQuery();
conn.Close();
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
推荐答案
你在catch(SqlException ex)
之前错过了一个}来终止try块。
顺便说一句:你为什么在下面的代码周围有一个区块
You're missing a "}" immediately before thecatch(SqlException ex)
to terminate the try block.
BTW: why do you have a block round the code immediately below
SqlCommand cmd=new SqlCommand("INSERT INTO persondetail VALUES (@name,@age,@salary)",conn);
您是否忘记使用
块启动?
[edit]
试试这个:
Did you forget the using
block start?
[edit]
Try this:
private void button1_Click(object sender, EventArgs e)
{
string name = textBox1.Text;
int age = Convert.ToInt32(textBox2.Text);
int salary = Convert.ToInt32(textBox3.Text);
try
{
string connectionstring = @"Data Source=THEONE\PARTH;Initial Catalog=testdatabase;Integrated Security=True;";
using (SqlConnection conn = new SqlConnection(connectionstring))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("INSERT INTO persondetail VALUES (@name,@age,@salary)", conn))
{
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@age", age);
cmd.Parameters.AddWithValue("@salary", salary);
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
[/ edit]
[/edit]
这篇关于看起来很完美但却给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文