使用asp.net和c#在sql server中存储和显示名称和地址字段的代码 [英] code for storing and displaying name and address fields in sql server with asp.net and c#

查看:76
本文介绍了使用asp.net和c#在sql server中存储和显示名称和地址字段的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都有代码用于存储和显示名称和地址字段,其中sql server用作数据库和asp.net和c#

我已经给出了以下代码..我感到很困惑数据库代码......请解释所有步骤..



do anybody have code for storing and displaying name and address field in which sql server is used as database and asp.net and c#
I have given the code below..I am getting confused about database code...and please explain all the steps..

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body style="height: 143px">
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="label1" runat="server">Name :</asp:Label>
    &nbsp;&nbsp;&nbsp;

        <asp:TextBox ID="TextBox1" runat="server" Height="14px" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

        <br />

        <br />
           <asp:Label ID="label2" runat="server">Address :</asp:Label>
    &nbsp;&nbsp;&nbsp;

        <asp:TextBox ID="TextBox2" runat="server" Height="14px" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

        <br />
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    </div>
    </form>
</body>
</html>

推荐答案

对不起,我不知道c#,但你可以从这段代码中得到这个想法。

我假设你的apsx页面上有文本框(txtFirst.txt txtlast.txt)控件和

按钮提交和gridview或其他东西给databind



将此代码放入Button click事件中。

用您自己的web.config文件中的连接字符串替换

用你自己的val替换字符串ues

用你的sql名称替换Table1表名





Sorry I dont know c# but you can get the idea from this code.
I'll Assume you have textbox (txtFirst.txt txtlast.txt) controls on your apsx page and a
Button to submit and gridview or something to databind

Put this code inside your Button click event.
Replace with your own connection string from your web.config file
Replace strings with your own values
Replace Table1 with the name of your sql Table name


Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Dim firstname, lastname  As String


        Try
            con.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ToString
            con.Open()
           
            cmd.Connection = con
            cmd.CommandText = "INSERT INTO Table1([FirstName], [LastName]) VALUES('" & firstname & "','" & lastname & "')"
            cmd.ExecuteNonQuery()

        Catch ex As Exception
            Response.Write("Error while inserting record on table..." + ex.Message + "Insert Records")
            
        Finally
            con.Close()
        End Try
        DataBind()


这是我从语言转换器工具获得的C#。



Here is C# that I got from language converter tool.

SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
string firstname = null;
string lastname = null;

try {
	con.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ToString;
	con.Open();

	cmd.Connection = con;
	cmd.CommandText = "INSERT INTO Table1([FirstName], [LastName]) VALUES('" + firstname + "','" + lastname + "')";
	cmd.ExecuteNonQuery();

} catch (Exception ex) {
	Response.Write("Error while inserting record on table..." + ex.Message + "Insert Records");

} finally {
	con.Close();
}
DataBind();


这篇关于使用asp.net和c#在sql server中存储和显示名称和地址字段的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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