怎么用vs2010编c语言写C语言程序

用VS2010写C语言程序的方法_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
用VS2010写C语言程序的方法
上传于||暂无简介
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩1页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢用VS2010做一个增删改查的小程序
整个程序如下:用户登录--进入用户文章页面--可对文章进行增删改
首先打开VS2010 文件--新建--网站
接着是数据库,这里用SQL2005
选择window验证登陆方式
进入数据库,点新建查询,建库加建表
create database Test use Test create table UserInfo
id int primary key identity(1,1),
username varchar(20),
password varchar(20)
) create table Article
id int primary key identity(1,1),
title varchar(50),
content varchar(100),
uid int FOREIGN KEY REFERENCES UserInfo(id)
insert into UserInfo values('test','123')
最后一句插入一条数据,用户名为test 密码为123
下面回到VS2010
在所选位置右键添加新项--选择类--名称定义为DBcon.cs
这时会提示是否放入APP_Code文件夹,选择是,就自动建了个文件夹,次文件夹只放类文件
双击DBcon.cs
using System.Collections.G
using System.L
using System.W
using System.D
using System.Data.SqlC
/// &summary&
///DBcon 的摘要说明
/// &/summary&
public class DBcon
private static SqlC
public DBcon()
//TODO: 在此处添加构造函数逻辑
public static SqlConnection getconnection()
sqlcon = new SqlConnection(&Data Source=.;Integrated Security=SSPI;Initial Catalog=Test&);
还有另一条SQL验证登录的语句
sqlcon = new SqlConnection(&server=.;uid=pwd=123456;database=Test&);
在项目新建一个web窗体 名称为Login.aspx
里面有个对应的cs文件 叫Login.aspx.cs
在 protected void Page_Load(object sender, EventArgs e)
{ }里添加如下代码,目的只是测试数据库是否能连接上
protected void Page_Load(object sender, EventArgs e)
SqlConnection sqlcon = DBcon.getconnection();
sqlcon.Open();
if (sqlcon.State == ConnectionState.Open)
Response.Write(&数据库连接成功&);
运行一下,如果窗体显示 数据库连接成功成功 那就是成功了
然后上面里面的代码也可以删除了
接着就是布局了,一个登录的页面很简单,拖拖控件就OK,但最好还是动手写代码
&html xmlns=&http://www.w3.org/1999/xhtml&&
&head runat=&server&&
&title&&/title&
&form id=&form1& runat=&server&&
&tr&&td&用户名:&/td&&td&&asp:TextBox ID=&txtusername& runat=&server&&&/asp:TextBox&&/td&&/tr&
&tr&&td&密码:&/td&&td&&asp:TextBox ID=&txtpassword& runat=&server& TextMode=&Password&&&/asp:TextBox&&/td&&/tr&
&tr&&td colspan=&2&&
&asp:Button ID=&btnLogin& runat=&server& Text=&登录& /&&/td&&/tr&
很简单的布局
双击按钮 进入事件代码
注:可能设计界面显示不出控件,这时候需要手动加代码
&asp:Button ID=&btnLogin& runat=&server& Text=&登录& onclick=&btnLogin_Click&/&
Login.aspx.cs如下
using System.Collections.G
using System.L
using System.W
using System.Web.UI;
using System.Web.UI.WebC
using System.D
using System.Data.SqlC public partial class Login : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void btnLogin_Click(object sender, EventArgs e)
if(this.txtusername.Text.Trim()==&& || this.txtpassword.Text.Trim()==&&)
Response.Write(&&script&alert('用户名或密码不能为空')&/script&&);
App_Code文件夹需要添加两个cs文件 Method.cs 和TestService.cs
Method.cs 如下
using System.Collections.G
using System.L
using System.W
using System.D
using System.Data.SqlC /// &summary&
///Method 的摘要说明
/// &/summary&
public class Method
public Method()
//TODO: 在此处添加构造函数逻辑
} /// &summary&
/// 对数据插入,更改,删除方法
/// &/summary&
/// &param name=&sql&&SQL语句&/param&
public static void DataMethod(string sql)
SqlConnection mycon = DBcon.getconnection();
mycon.Open();
SqlCommand cmd = new SqlCommand(sql, mycon);
cmd.ExecuteNonQuery();
catch (Exception ex)
throw (ex);
mycon.Dispose();
mycon.Close();
/// &summary&
/// 检查数据
/// &/summary&
/// &param name=&sql&&&/param&
/// &returns&&/returns&
public static bool DataCheak(string sql)
SqlConnection mycon = DBcon.getconnection();
mycon.Open();
SqlCommand cmd = new SqlCommand(sql, mycon);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
dr.Dispose();
cmd.Dispose();
mycon.Dispose(); } /// &summary&
/// 以DataSet获取数据
/// &/summary&
/// &param name=&sql&&&/param&
/// &param name=&table&&&/param&
/// &returns&&/returns&
public static DataSet GetData(string sql, string table)
SqlConnection mycon = DBcon.getconnection();
SqlCommand cmd = new SqlCommand(sql, mycon);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, table);
catch (Exception ex)
throw (ex);
mycon.Dispose();
mycon.Close();
public static SqlDataReader GetData(string sql)
SqlConnection mycon = DBcon.getconnection();
mycon.Open();
SqlCommand cmd = new SqlCommand(sql, mycon);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
catch (Exception ex)
throw (ex);
TestService.cs如下
using System.Collections.G
using System.L
using System.W
using System.D
using System.Data.SqlC /// &summary&
///TestService 的摘要说明
/// &/summary&
public class TestService
public TestService()
//TODO: 在此处添加构造函数逻辑
public Boolean Login(string username,string password)
String sql = &select * from UserInfo where username='&+username+&' and password='&+password+&'&;
if (Method.DataCheak(sql))
public DataSet ShowList(int Userid)
String sql = &select * from Article where uid=&+Userid+&&;
DataSet ds = Method.GetData(sql, &list&);
} public DataSet GetUser(string username, string password)
String sql = &select * from UserInfo where username='&+username+&' and password='&+password+&'&;
DataSet ds = Method.GetData(sql, &UserInfo&);
} public void Add(string title, string content,int uid)
String sql = string.Format(&insert into Article values('{0}','{1}',{2})&, title, content, uid);
Method.DataMethod(sql);
} public SqlDataReader GetOneList(int id)
String sql = &select * from Article where id=&+id+&&;
SqlDataReader dr = Method.GetData(sql);
} public void Update(string title, string content, int id)
String sql = &update Article set title='& + title + &',content='& + content + &' where id=& + id + & &;
Method.DataMethod(sql);
} public void Delete(int id)
String sql = &delete from Article where id=& + id + & &;
Method.DataMethod(sql);
Login.aspx.cs 最终如下
using System.Collections.G
using System.L
using System.W
using System.Web.UI;
using System.Web.UI.WebC
using System.D
using System.Data.SqlC public partial class Login : System.Web.UI.Page
TestService ts = new TestService();
protected void Page_Load(object sender, EventArgs e)
protected void btnLogin_Click(object sender, EventArgs e)
if(this.txtusername.Text.Trim()==&& || this.txtpassword.Text.Trim()==&&)
Response.Write(&&script&alert('用户名或密码不能为空')&/script&&);
else if (!ts.Login(this.txtusername.Text.Trim(), this.txtpassword.Text.Trim()))
Response.Write(&&script&alert('用户名或密码错误')&/script&&);
DataSet ds = ts.GetUser(this.txtusername.Text.Trim(), this.txtpassword.Text.Trim());
Session[&UID&] = Convert.ToInt32(ds.Tables[&UserInfo&].Rows[0][0].ToString());
Session[&Username&] = ds.Tables[&UserInfo&].Rows[0][1].ToString();
Response.Write(&&script&alert('登录成功');location.href='ArticleList.aspx'&/script&&);
添加页面ArticleList.aspx&
&%@ Page Language=&C#& AutoEventWireup=&true& CodeFile=&ArticleList.aspx.cs& Inherits=&ArticleList& %& &!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&&/title&
&form id=&form1& runat=&server&&
&p&用户名:&asp:Label ID=&Label1& runat=&server& Text=&&&&/asp:Label&&/p&
&asp:DataList ID=&Dl_List& runat=&server& DataKeyField=&id&&
onitemcommand=&Dl_List_ItemCommand&&
&ItemTemplate&
&a href=&Update.aspx?id=&%# Eval(&id&)%& && &%# Eval(&title&)%& &/a&&
&asp:LinkButton ID=&LB_Del& runat=&server& CommandName=&delete&&删除&/asp:LinkButton&
&/ItemTemplate&
&/asp:DataList&
&div id=&sorry& style=&display:none& runat=&server&&
&p style=& margin-left:10color:Rfont-weight:bold&&对不起,你还没有文章&/p&
&asp:LinkButton ID=&LB_Add& runat=&server& onclick=&LB_Add_Click& &添加&/asp:LinkButton&
using System.Collections.G
using System.L
using System.W
using System.Web.UI;
using System.Web.UI.WebC
using System.D
using System.Data.SqlC public partial class ArticleList : System.Web.UI.Page
TestService ts = new TestService();
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
this.Label1.Text = Session[&Username&].ToString();
} public void DLBind()
DataSet ds = ts.ShowList(int.Parse(Session[&UID&].ToString()));
this.Dl_List.DataSource = ds.Tables[&List&].DefaultV
this.Dl_List.DataBind();
if (ds.Tables[0].Rows.Count &= 0)
sorry.Style[&display&] = &block&;
protected void LB_Add_Click(object sender, EventArgs e)
Response.Redirect(&Add.aspx&);
protected void Dl_List_ItemCommand(object source, DataListCommandEventArgs e)
switch (e.CommandName)
case &delete&:
ts.Delete(int.Parse(Dl_List.DataKeys[e.Item.ItemIndex].ToString()));
Response.Write(&&script&alert('删除成功')&/script&&);
添加页面Add.aspx
&%@ Page Language=&C#& AutoEventWireup=&true& CodeFile=&Add.aspx.cs& Inherits=&Add& %& &!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&&/title&
&form id=&form1& runat=&server&&
&p&添加文章&/p&
标题:&asp:TextBox ID=&txtTitle& runat=&server&&&/asp:TextBox&&br /&
内容:&asp:TextBox ID=&txtContent& runat=&server& TextMode=&MultiLine&&&/asp:TextBox&&br /&
&asp:Button ID=&btnAdd& runat=&server& Text=&添加& onclick=&btnAdd_Click& /&
using System.Collections.G
using System.L
using System.W
using System.Web.UI;
using System.Web.UI.WebC public partial class Add : System.Web.UI.Page
TestService ts = new TestService();
protected void Page_Load(object sender, EventArgs e)
protected void btnAdd_Click(object sender, EventArgs e)
string title=this.txtTitle.Text.Trim();
string content=this.txtContent.Text.Trim();
int uid=int.Parse(Session[&UID&].ToString());
ts.Add(title, content, uid);
Response.Write(&&script&alert('添加成功');location.href='ArticleList.aspx'&/script&&);
添加页面Update.aspx
&%@ Page Language=&C#& AutoEventWireup=&true& CodeFile=&Update.aspx.cs& Inherits=&Update& %& &!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&&/title&
&form id=&form1& runat=&server&&
&p&修改文章&/p&
标题:&asp:TextBox ID=&txtTitle& runat=&server&&&/asp:TextBox&&br /&
内容:&asp:TextBox ID=&txtContent& runat=&server& TextMode=&MultiLine&&&/asp:TextBox&&br /&
&asp:Button ID=&btnUpdate& runat=&server& Text=&修改& onclick=&btnUpdate_Click& /&
using System.Collections.G
using System.L
using System.W
using System.Web.UI;
using System.Web.UI.WebC
using System.D
using System.Data.SqlC public partial class Update : System.Web.UI.Page
TestService ts = new TestService();
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
ListBind(Convert.ToInt32(Request.QueryString[&id&].ToString())); }
} public void ListBind(int id)
SqlDataReader dr = ts.GetOneList(id);
if (dr.Read())
this.txtTitle.Text = dr[&title&].ToString();
this.txtContent.Text = dr[&content&].ToString();
protected void btnUpdate_Click(object sender, EventArgs e)
ts.Update(this.txtTitle.Text, this.txtContent.Text, Convert.ToInt32(Request.QueryString[&id&].ToString()));
Response.Write(&&script&alert('修改成功');location.href='ArticleList.aspx'&/script&&);
下一篇:没有了
相关内容:
编程爱好者
WEB编程开发C语言编程感想(2)
今天就开始我的第一个C的编程吧~
1.&& 如何用visual studio 2010 新建一个C程序项目
首先,打开VS2010 ,点击“文件/新建/项目”或者用快捷键“Ctrl+Shift+N”,之后会弹出下面图示:
我们选择黄色着重表示的win32控制台应用程序(win32 Console Application),输入名称“project_001”,点击确定
之后弹出如下图示:
单击“下一步”
在红圈处,勾选“空项目“
点击”完成“就可以了
在之后“解决方案管理器”中,“源文件”处点击右键“添加/新建项”(Ctrl+Shift+A)
在弹出的对话框中选择“C++文件”,“名称”中输入的后缀,一定一定要是“.c”,记住,“.”这个点不能丢。如果没有写后缀,系统默认是“.cpp”&即C++文件。
最后点击“添加”,这样一个工程文件就添加成功了!
2. 编写一个C 小程序
一开始用“#”表示 预处理命令。为了与一般的C语句区别,预处理命令以“#”开头,“#”号必须是该行除了任何空白字符外的第一个字符,“#”后是指令关键字,在关键字和#号之间允许存在任意个数的空白字符,整行语句构成了一条预处理指令。
为什么要用预处理命令呢?我在CSDN的另一篇博客中查找到了关于预处理命令的详细解释(之后我会转发该作者的博文,以此共享),简单来说就是引用已经存在的包裹来完成我的这个程序包裹,代码就更简洁,可用性更强了~“#include”就是指把后面的内容引用进来,即 文件包含处理。“stdlib.h”&stdio.h&都是常用的函数,之后将会详细解释。
&int main&之下,用“{}”一对花括号将主函数里要编写的内容包含起来,打个包吧就是,“int ”指其返回内容是一个整形数,体现在“return 0”—返回值为0。“printf (&&& &)”指输出内容将会在括号家引号里显示出来。在每一句编码的最后,都要用“;”结束这句代码,多多练习就会形成这个每句话结束加分号的好习惯的。还有,我们应该养成勤于在关键处写注释的好习惯,我的这个程序就没有写,行注释用“//”开头就可以了,这也将会在我的后面文章中详细笔记的。
3.编译生成这个程序
在“调试”菜单栏中选择“开始执行(不调试)”或是“Ctrl+F5” 就会生成上述图片中右边部分的黑屏“.exe” 文件,这样,一个C语言小程序就完成啦!
后记:今天开始写我的第一个C语言小程序,写这么多就是希望自己能将编程坚持下去,每天记录我的成长,我的笔记,我的SE的路程。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:2065次
排名:千里之外
(1)(1)(1)(1)(4)

我要回帖

更多关于 vs2010调试c语言程序 的文章

 

随机推荐