【发布时间】:2014-09-05 19:14:02
【问题描述】:
我有一个名为 Table1 的 sql 表,它有 3 列 - Name(string)、Value(float)、Date(datetime)。我想在单击 Button1 时填充表。 所以我有一个“插入”函数,它填充表格和按钮单击函数,从两个文本框获取值。问题是,当我像解释的那样调用“插入”函数时,它给了我一些参数无效的错误消息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Services;
using System.Configuration;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod]
public void Insert(string Name, float Value, DateTime DateTime)
{
string connectionString = ConfigurationManager.ConnectionStrings["FatherDB"].ToString();
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("Insert into Table1 ((Name) values(@Name) ,(Value) values(@Value),(Date) values(@Date))", con))
{
con.Open();
cmd.Parameters.AddWithValue("@Name", Name);
cmd.Parameters.AddWithValue("@Value", Value);
cmd.Parameters.AddWithValue("@Date", DateTime);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DateTime z = DateTime.Now;
string Name = Box1.ToString();
float Value = float.Parse(Box2.Text);
Insert(Name, Value, z);
//Insert(Name, Value, z); want to call the function with parameters from text boxes
}
}
}
HTML页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<script type="text/javascript">
</script>
<style>
.TBox{
position:relative;
height:30px;
width:150px;
margin-left:10px;
}
.Div {
margin-top:50px;
}
</style>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div class="Div">
<asp:TextBox id="Box1" CssClass="TBox" runat="server">
</asp:TextBox>
<asp:TextBox id="Box2" CssClass="TBox" runat="server">
</asp:TextBox>
<asp:Button id="Button1" CssClass="TBox" runat="server" Text="Enter" />
</div>
</form>
</body>
</html>
【问题讨论】:
-
错误信息到底是什么?
-
现在可以正常工作,但表格没有填满
-
@Arch_interpreter,确保您正在检查正确的数据库。可能你有多个数据库用于开发和生产等
-
连接字符串设置正确,我只有那个数据库,我很好奇有什么问题。每次我单击按钮时,它都会调用该函数,但是当我单击显示表数据时,它是空的
-
@Arch_interpreter,您是否尝试在 SQL Server Management Studio 中查看数据?
标签: c# html asp.net sql sql-server