表1要插入数据,里面的sno值必须是表2里有的,且表1不可重复插入数据 插入数据前先判断表1表2里是不是有对应sno值的数据呗 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 insert into 表1(sno) select sno from 表2 where 条件 string strsno = TextBox3.Text.Trim(); string strkno = DropDownList4.SelectedValue.Trim(); string strcredit = DropDownList5.SelectedValue.Trim(); string strc = TextBox7.Text.Trim(); try { string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ; myCon = new SqlConnection(myConStr); myCon.Open(); myCom = new SqlCommand(); myCom.Connection = myCon; string strSelect = "select * from sc where sno='{0}'and kno='{1}'"; string sql = string.Format(strSelect, strsno, strkno); myCom.CommandType = CommandType.Text; myCom.CommandText = sql; myDa = new SqlDataAdapter(); myDa.SelectCommand = myCom; myDs = new DataSet(); myDa.Fill(myDs, "DS_MMB"); if (myDs.Tables["DS_MMB"].Rows.Count <= 0) { myCon = new SqlConnection(myConStr); myCon.Open(); myCom = new SqlCommand(); myCom.Connection = myCon; string strInsert = "insert into sc(sno,kno,credit,c) values('{0}','{1}','{2}','{3}')"; sql = string.Format(strInsert, strsno, strkno, strcredit, strc); myCom.CommandType = CommandType.Text; myCom.CommandText = sql; myCom.ExecuteNonQuery(); Response.Write("<script language=javascript>alert('数据添加成功!')</script>"); } else { Label3.Text = "信息已经存在"; } 这是我的代码,这个是不可以重复添加的,但是我该怎么弄个添加学号时,必须是student表里有的。表1名字是score 表2名字是student 像这种情况,学号那里就不能用文本框了吧,用个下拉列表,从表student里面把学号读出来,然后选择哪个添加哪个,这样就不用判断学号是否重复添加了,,,只判断数据是否重复就可以了 C#中父类和子类对象之间强制装换的问题 PictureBox的一点问题 WinForm中如何根据DataTable动态生成TreeView 用了两个绑定,为什么前台页面说我没有字段值呢? Winform DataGrid 样式存储问题. 超难的问题:高手请进 菜鸟求教:C#中如何调用IE打开*.htm文件! C# 加载html找不到js 求助 关于DateTime的XML序列化 如何发送表单? 求大神把一小段JAVA代码改成C#代码,谢谢! C# Winform Panel 拖动图片到Panel外
string strkno = DropDownList4.SelectedValue.Trim();
string strcredit = DropDownList5.SelectedValue.Trim();
string strc = TextBox7.Text.Trim();
try
{
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
myCom = new SqlCommand();
myCom.Connection = myCon;
string strSelect = "select * from sc where sno='{0}'and kno='{1}'";
string sql = string.Format(strSelect, strsno, strkno);
myCom.CommandType = CommandType.Text;
myCom.CommandText = sql;
myDa = new SqlDataAdapter();
myDa.SelectCommand = myCom;
myDs = new DataSet();
myDa.Fill(myDs, "DS_MMB");
if (myDs.Tables["DS_MMB"].Rows.Count <= 0)
{
myCon = new SqlConnection(myConStr);
myCon.Open();
myCom = new SqlCommand();
myCom.Connection = myCon;
string strInsert = "insert into sc(sno,kno,credit,c) values('{0}','{1}','{2}','{3}')";
sql = string.Format(strInsert, strsno, strkno, strcredit, strc);
myCom.CommandType = CommandType.Text;
myCom.CommandText = sql;
myCom.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('数据添加成功!')</script>");
}
else
{
Label3.Text = "信息已经存在";
}
表1名字是score 表2名字是student