数据库中表的插入的相关问题,高手指教!!!!!!!!!!!!!!!!!! 用DropDownList控件试试看,它有一个Text和一个Value的。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 第一种方法就是一楼的那个,初始化数据的时候你就可以绑定成text和value,一个显示,一个作为值。如果楼主不像用droplist,就是要用textbox,那楼主可以先找到这个name对应的id在插入好了,呵呵,我想楼主还是会用droplist的对吧 SqlConnection Conn = new SqlConnection( "server=localhost;database=bbsno1;uid=sa;pwd=sa" );string SQL_Select = "select id, ItemName from TABLE1 order by id desc";//构造一个SqlDataAdapterSqlDataAdapter myAdapter = new SqlDataAdapter( SQL_Select, Conn);//开始读取数据Conn.Open();DataSet dataSet = new DataSet();myAdapter.Fill( dataSet,"Table1" );Conn.Close();//开始绑定DropDownList//指定DropDownList使用的数据源ddlArea.DataSource = dataSet.Tables["Table1"].DefaultView;//指定DropDownList使用的表里的那些字段ddlArea.DataTextField = "area_name"; //dropdownlist的Text的字段ddlArea.DataValueField = "area_id";//dropdownlist的Value的字段ddlArea.DataBind(); ddlArea.SelectedValue就是外键id,接下去数据库的插入操作你应该会吧? asp.net夜话之八:数据绑定控件在asp.net中所有的数据库绑定控件都是从BaseDataBoundControl这个抽象类派生的,这个抽象类定义了几个重要属性和一个重要方法:DataSource属性:指定数据绑定控件的数据来源,显示的时候程序将会从这个数据源中获取数据并显示。DataSourceID属性:指定数据绑定控件的数据源控件的ID, 显示的时候程序将会根据这个ID找到相应的数据源控件,并利用这个数据源控件中指定方法获取数据并显示。DataBind()方法:当指定了数据绑定控件的DataSource属性或者DataSourceID属性之后,再调用DataBind()方法才会显示绑定的数据。并且在使用数据源时,会首先尝试使用DataSourceID属性标识的数据源,如果没有设置DataSourceID时才会用到DataSource属性标识的数据源。也就是说DataSource和DataSourceID两个属性不能同时使用。数据绑定控件的DataSource控件属性必须是一个可以枚举的数据源,如实现了ICollection、IEnumerable或IListSource接口的类的实例。 private void BindUserList() { //实例化Connection对象 SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa"); //实例化Command对象 SqlCommand command = new SqlCommand("select UserID,RealName from UserInfo", connection); SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable data = new DataTable(); adapter.Fill(data); ddlUserList.DataTextField = "RealName";//指定下拉列表中的文字显示部分 ddlUserList.DataValueField = "UserID";//指定下拉列表中的值部分 ddlUserList.DataSource = data; ddlUserList.DataBind(); } Click the link to solve your problem.Good luck! 1.先绑定表A,页面上放个dorpdownlist把下面这个方法放到page_load事件里加if(!ispostback){Bind();}private void Bind() { SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=你的数据库名;Persist Security Info=True;User ID=sa;Password=sa"); SqlDataAdapter adapter = new SqlDataAdapter(select * from area", connection); DataSet ds = new DataSet(); adapter.Fill(ds,"area"); DataTable dt = ds.Table[0].DefultView; ddl.DataTextField = "rea_name";//指定下拉列表中的文字显示部分 ddl.DataValueField = "area_id";//指定下拉列表中的值部分 ddl.DataSource = data; ddl.DataBind(); } 2 取公司信息和ddl的值SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=你的数据库名;Persist Security Info=True;User ID=sa;Password=sa"); connection.Open();string strCommpanyInfo = txtcor_name.Text.Trim();string strCommpanyareid ddl.SelectedValue;string strsql = "insert into company(com_name,area_id)values('" + strCommpanyInfo + "','" +strCommpanyareid +"')");SqlCommand cmd = new SqlCommand(strsql,connection);cmd.ExecuteNonQuery();connection.Close();好了给你写完了直接就能用了 在学ajax遇到的问题 晕了,做的第一个asp.net页面,怎样table居中?? sql 关联表问题 高手帮个忙,这样的问题。。 求高手们帮忙解决一下数据绑定的问题 查询问题(使用where 1=1) 问,如何定位到页面上的某一个控件? 如何从数据库中读去图片并生成文件? Application和session的默认时间分别是多少?在那设置? .net中如何做网络打印?请给个思路吧 谁有meizz jsframework: 轮换广告图的DEMO 里面的CSS 急求ASp问题,高手帮忙
如果楼主不像用droplist,就是要用textbox,那楼主可以先找到这个name对应的id在插入好了,呵呵,我想楼主还是会用droplist的对吧
string SQL_Select = "select id, ItemName from TABLE1 order by id desc";
//构造一个SqlDataAdapter
SqlDataAdapter myAdapter = new SqlDataAdapter( SQL_Select, Conn);
//开始读取数据
Conn.Open();
DataSet dataSet = new DataSet();
myAdapter.Fill( dataSet,"Table1" );
Conn.Close();
//开始绑定DropDownList
//指定DropDownList使用的数据源
ddlArea.DataSource = dataSet.Tables["Table1"].DefaultView;
//指定DropDownList使用的表里的那些字段
ddlArea.DataTextField = "area_name"; //dropdownlist的Text的字段
ddlArea.DataValueField = "area_id";//dropdownlist的Value的字段
ddlArea.DataBind();
ddlArea.SelectedValue就是外键id,接下去数据库的插入操作你应该会吧?
在asp.net中所有的数据库绑定控件都是从BaseDataBoundControl这个抽象类派生的,这个抽象类定义了几个重要属性和一个重要方法:DataSource属性:指定数据绑定控件的数据来源,显示的时候程序将会从这个数据源中获取数据并显示。DataSourceID属性:指定数据绑定控件的数据源控件的ID, 显示的时候程序将会根据这个ID找到相应的数据源控件,并利用这个数据源控件中指定方法获取数据并显示。DataBind()方法:当指定了数据绑定控件的DataSource属性或者DataSourceID属性之后,再调用DataBind()方法才会显示绑定的数据。并且在使用数据源时,会首先尝试使用DataSourceID属性标识的数据源,如果没有设置DataSourceID时才会用到DataSource属性标识的数据源。也就是说DataSource和DataSourceID两个属性不能同时使用。数据绑定控件的DataSource控件属性必须是一个可以枚举的数据源,如实现了ICollection、IEnumerable或IListSource接口的类的实例。
private void BindUserList()
{
//实例化Connection对象
SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa");
//实例化Command对象
SqlCommand command = new SqlCommand("select UserID,RealName from UserInfo", connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable data = new DataTable();
adapter.Fill(data); ddlUserList.DataTextField = "RealName";//指定下拉列表中的文字显示部分
ddlUserList.DataValueField = "UserID";//指定下拉列表中的值部分
ddlUserList.DataSource = data;
ddlUserList.DataBind();
}
{
SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=你的数据库名;Persist Security Info=True;User ID=sa;Password=sa");
SqlDataAdapter adapter = new SqlDataAdapter(select * from area", connection);
DataSet ds = new DataSet();
adapter.Fill(ds,"area");
DataTable dt = ds.Table[0].DefultView;
ddl.DataTextField = "rea_name";//指定下拉列表中的文字显示部分
ddl.DataValueField = "area_id";//指定下拉列表中的值部分
ddl.DataSource = data;
ddl.DataBind();
}
2 取公司信息和ddl的值SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=你的数据库名;Persist Security Info=True;User ID=sa;Password=sa");
connection.Open();
string strCommpanyInfo = txtcor_name.Text.Trim();
string strCommpanyareid ddl.SelectedValue;
string strsql = "insert into company(com_name,area_id)values('" + strCommpanyInfo + "','" +strCommpanyareid +"')");
SqlCommand cmd = new SqlCommand(strsql,connection);
cmd.ExecuteNonQuery();
connection.Close();好了给你写完了直接就能用了