【发布时间】:2014-03-30 13:37:53
【问题描述】:
大家好,我正在尝试将从前端获得的值插入到 sql server 数据库中。这是我的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Geocoding.Google;
using Geocoding;
using System.Data.SqlClient;
public partial class regforswa : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string lat;
string longi;
IGeocoder geocoder = new GoogleGeocoder() { };
Address[] addresses =
geocoder.Geocode(TextBox4.Text+TextBox5.Text+TextBox6.Text).ToArray();
foreach (Address adrs in addresses)
{
//Response.Write("lat=" +adrs.Coordinates.Latitude.ToString());
//Response.Write( "longi="+ adrs.Coordinates.Longitude.ToString());
}
SqlConnection conn = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=swa1;User Id=swa1;Password=swa1;");//datasource=localhost name,initial catalog=db name
//integrated security=windiws authentication OR for sql authentication specify
//userid and password
conn.Open();//opens connection with the database
try
{//exeption handling
String uname=TextBox1.Text;
String pwd=TextBox3.Text;
String mnumber = TextBox7.Text;
String sques = DropDownList1.Text;
String res = TextBox8.Text;
String adress = TextBox4.Text;
String cty = TextBox5.Text;
String zpcode = TextBox6.Text;
// Response.Write("uname"+uname +"pwd"+ pwd);
SqlCommand newcomm = new SqlCommand("INSERT INTO regforswa(username,password,mno,sq,response,address,city,zipcode) VALUES (uname,pwd,mnumber,sques,res,adress,cty,zpcode)", conn);
if (newcomm.ExecuteNonQuery() == 1)
{
result.Visible = true;
result.Text = "entered successfully";
}
else
{
result.Text = "not entered";
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
}
}
但我收到此错误
列名“uname”无效。列名“pwd”无效。列无效 名称'mnumber'。列名“sques”无效。列名无效 'res'。列名“地址”无效。列名“cty”无效。 列名“zpcode”无效。
我检查了我的数据库列名,它是完美的。请有人帮忙。为什么我会收到这个错误?
【问题讨论】:
标签: c# sql-server visual-studio-2010