如何通过检查另一个表将数据从Gridview插入数据库 [英] How to insert data from Gridview to database by checkiing with another Table

查看:57
本文介绍了如何通过检查另一个表将数据从Gridview插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码我将XML数据导入数据库。

This below code i had followed to import XML data in to DB.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head  runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    <asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click" 

        Text="Insert Records" />
    <asp:Button ID="btn_show" runat="server" onclick="btn_show_Click" 

        style="height: 26px" Text="Show Records" />
    </div>
    </form>
</body>
</html>







using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Insert_data_from_Gridview_database
{
    public partial class _Default : System.Web.UI.Page
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlCommand com;

        protected void btn_show_Click(object sender, EventArgs e)
        {
            DataSet reportData = new DataSet();
            reportData.ReadXml(Server.MapPath("student.xml"));
            GridView1.DataSource = reportData;
            GridView1.DataBind();
        }

        protected void btn_insert_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow g1 in GridView1.Rows)
            {
                SqlConnection con = new SqlConnection(connStr);
                com = new SqlCommand("insert into student(sid,sname,smarks,saddress) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "')", con);
                con.Open();
                com.ExecuteNonQuery();
                con.Close();

            }
            Label1.Text = "Records inserted successfully";
        }
    }
}



还有一个帮助。



桌子

学生(sid,sname,smarks,saddress)//这里我们从XML插入到DB

master_table(sid,sname,age,gender)这些r这两个表



这里我想再检查一下,比如,如果XML文件中的sid不在master_table中,那么我想用不同的颜色显示那些在网格视图中,当我点击显示按钮。



发布解决方案对于这篇文章

先谢谢你


One more Help in this.

Tables
student(sid,sname,smarks,saddress) //Here we insert from XML to DB
master_table(sid,sname,age,gender) these r the two tables

Here i want to check one more thing, i.e., if sid in XML file is not in the master_table means then i want to show those in different colour in the grid view while i click the show button.

Post Solution For this post
Thanks in Advance

推荐答案

只需在sqlcommand中使用select查询。

并使用dataReader。

sqldatareader dr = cmd.executereader(); //(cmd是Sqlcommand的对象)

if(dr.read())

{

//在这里使用你的代码

}
just use select query in sqlcommand.
and use dataReader.
sqldatareader dr = cmd.executereader(); //(cmd is object of Sqlcommand)
if (dr.read())
{
//use your code here
}


这篇关于如何通过检查另一个表将数据从Gridview插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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