ASP.net 后台sql语句

omarlai 2016-04-20 04:09:47
string conn = "Data Source=.\\SQLExpress;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Database1.mdf;User Instance=true";




string strsql = "insert into Wechat(StudentName,ClassID,word,username)(select StudentName,ClssID from Student where username=session['username1']),TextBox1.Text,session['username1'])";
DataSet ds = new DataSet();
SqlConnection connection = new SqlConnection(conn);
connection.Open();
SqlDataAdapter Cmd = new SqlDataAdapter(strsql, connection);
Cmd.Fill(ds, "Wechat");

请问为什么我这个sql语句一直报错?
...全文
211 8 打赏 收藏 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
omarlai 2016-04-20
  • 打赏
  • 举报
回复
引用 7 楼 lovelj2012 的回复:
string userName = Session['username1'] != null ? Session['username1'].ToString().Trim() : "";
if(String.IsNullOrEmpty(userName))
     return;//如果userName不存在,没必要往下执行语句了
string str = TextBox1.Text.Trim();
 string strsql = "insert into Wechat(StudentName,ClassID,word,username) select StudentName,ClssID,'"+str+"', '"+userName+"'  from Student where username='"+userName +"'";
撸主先学习下基础吧
谢谢了,现在OK了,真的感谢~!
江南小鱼 2016-04-20
  • 打赏
  • 举报
回复
string userName = Session['username1'] != null ? Session['username1'].ToString().Trim() : "";
if(String.IsNullOrEmpty(userName))
     return;//如果userName不存在,没必要往下执行语句了
string str = TextBox1.Text.Trim();
 string strsql = "insert into Wechat(StudentName,ClassID,word,username) select StudentName,ClssID,'"+str+"', '"+userName+"'  from Student where username='"+userName +"'";
撸主先学习下基础吧
omarlai 2016-04-20
  • 打赏
  • 举报
回复
引用 5 楼 lovelj2012 的回复:
[quote=引用 4 楼 omarlai 的回复:] [quote=引用 3 楼 lovelj2012 的回复:]

string userName = Session['username1'] != null ? Session['username1'].ToString().Trim() : "";
if(String.IsNullOrEmpty(userName))
     return;//如果userName不存在,没必要往下执行语句了
 string strsql = "insert into Wechat(StudentName,ClassID,word,username) select StudentName,ClssID,TextBox1.Text, '"+userName+"'  from Student where username='"+userName +"'";
改成↑ ps:可能有错,手敲的代码
为什么userName前后要加+?还是不行哦[/quote] 你debug一下,拿到strsql的值,贴出来看看~[/quote] 是null,另外TextBox1.Text是我从网页获取的,不是从表里搜索的,也写在select里面吗?
江南小鱼 2016-04-20
  • 打赏
  • 举报
回复
引用 4 楼 omarlai 的回复:
[quote=引用 3 楼 lovelj2012 的回复:]

string userName = Session['username1'] != null ? Session['username1'].ToString().Trim() : "";
if(String.IsNullOrEmpty(userName))
     return;//如果userName不存在,没必要往下执行语句了
 string strsql = "insert into Wechat(StudentName,ClassID,word,username) select StudentName,ClssID,TextBox1.Text, '"+userName+"'  from Student where username='"+userName +"'";
改成↑ ps:可能有错,手敲的代码
为什么userName前后要加+?还是不行哦[/quote] 你debug一下,拿到strsql的值,贴出来看看~
omarlai 2016-04-20
  • 打赏
  • 举报
回复
引用 3 楼 lovelj2012 的回复:

string userName = Session['username1'] != null ? Session['username1'].ToString().Trim() : "";
if(String.IsNullOrEmpty(userName))
     return;//如果userName不存在,没必要往下执行语句了
 string strsql = "insert into Wechat(StudentName,ClassID,word,username) select StudentName,ClssID,TextBox1.Text, '"+userName+"'  from Student where username='"+userName +"'";
改成↑ ps:可能有错,手敲的代码
为什么userName前后要加+?还是不行哦
江南小鱼 2016-04-20
  • 打赏
  • 举报
回复

string userName = Session['username1'] != null ? Session['username1'].ToString().Trim() : "";
if(String.IsNullOrEmpty(userName))
     return;//如果userName不存在,没必要往下执行语句了
 string strsql = "insert into Wechat(StudentName,ClassID,word,username) select StudentName,ClssID,TextBox1.Text, '"+userName+"'  from Student where username='"+userName +"'";
改成↑ ps:可能有错,手敲的代码
omarlai 2016-04-20
  • 打赏
  • 举报
回复
引用 1 楼 lovelj2012 的回复:
你debug一下,拿到strsql的值,然后去数据库执行下,哪里报错不就一目了然了嘛
我查了,是null值,怎么办?
江南小鱼 2016-04-20
  • 打赏
  • 举报
回复
你debug一下,拿到strsql的值,然后去数据库执行下,哪里报错不就一目了然了嘛

111,128

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧