资源简介
可供初学者好好学习一下的,这是本人花了五天时间做出来的,虽然不怎么样但是还是感觉很有收获的。大家可借鉴一下。
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace day6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender EventArgs e)
{
}
string constr = “Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True“;
private void button1_Click(object sender EventArgs e)
{
SqlConnection con = new SqlConnection(constr);//效果与下面两句一样只要连接字符串正确就不会报错,运行时错误
//SqlConnection con = new SqlConnection(constr);
//con.ConnectionString = constr;
SqlCommand cmd = new SqlCommand();//实例化SQL对象
cmd.Connection = con;//数据库连接和命令连接起来
cmd.CommandText = “select idstuname sex age from stutable“;//stable必须在MyDatabase中获取两列
try
{
con.Open();//在执行时遇到问题才报错,编译时报错
SqlDataReader dr = cmd.ExecuteReader();
listBox1.Items.Clear();
listBox2.Items.Clear();
listBox3.Items.Clear();
listBox4.Items.Clear();
while (dr.Read())
{
listBox1.Items.Add(dr.GetValue(0).ToString());
listBox2.Items.Add(dr.GetValue(1).ToString());
listBox3.Items.Add(dr.GetValue(2).ToString());
listBox4.Items.Add(dr.GetValue(3).ToString());
//dr.GetValue(0);
//dr.GetValue(1);上面只获取两列所以只取两列
//MessageBox.Show(dr.GetValue(0).ToString());
//MessageBox.Show(dr.GetValue(1).ToString());
}
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
private void label1_Click(object sender EventArgs e)
{
}
private void button2_Click(object sender EventArgs e)
{
string sex = ““;
if (radioButton1.Checked)
{
sex = “男“;
}
else
{
sex = “女“;
}
string sql = “insert into stutable(stunamesexage)values(‘“+textBox1.Text+“‘‘“+sex+“‘‘“+textBox2.Text+“‘)“;
//ID是自己增加不需要添加
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = sql;
try
{
con.Open();
cmd.ExecuteNonQuery();//不是执行查询都可以使用这个语句返回影响的行数
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 28672 2010-03-21 16:23 实习\c#\Projects\1day6简便试修改\1day6\bin\Debug\day6.exe
文件 34304 2010-03-21 16:23 实习\c#\Projects\1day6简便试修改\1day6\bin\Debug\day6.pdb
文件 5632 2005-12-08 14:51 实习\c#\Projects\1day6简便试修改\1day6\bin\Debug\day6.vshost.exe
文件 3554 2010-03-21 15:28 实习\c#\Projects\1day6简便试修改\1day6\day6.csproj
文件 5320 2010-03-21 15:32 实习\c#\Projects\1day6简便试修改\1day6\Form1.cs
文件 12763 2010-03-21 15:32 实习\c#\Projects\1day6简便试修改\1day6\Form1.Designer.cs
文件 5814 2010-03-21 15:32 实习\c#\Projects\1day6简便试修改\1day6\Form1.resx
文件 1550 2010-03-21 16:23 实习\c#\Projects\1day6简便试修改\1day6\Form2.cs
文件 4821 2010-03-21 16:23 实习\c#\Projects\1day6简便试修改\1day6\Form2.Designer.cs
文件 7286 2010-03-21 16:23 实习\c#\Projects\1day6简便试修改\1day6\Form2.resx
文件 279 2010-03-21 16:24 实习\c#\Projects\1day6简便试修改\1day6\obj\day6.csproj.FileList.txt
文件 900 2010-03-21 16:23 实习\c#\Projects\1day6简便试修改\1day6\obj\Debug\day6.csproj.GenerateResource.Cache
文件 28672 2010-03-21 16:23 实习\c#\Projects\1day6简便试修改\1day6\obj\Debug\day6.exe
文件 180 2010-03-21 15:32 实习\c#\Projects\1day6简便试修改\1day6\obj\Debug\day6.Form1.resources
文件 180 2010-03-21 16:23 实习\c#\Projects\1day6简便试修改\1day6\obj\Debug\day6.Form2.resources
文件 34304 2010-03-21 16:23 实习\c#\Projects\1day6简便试修改\1day6\obj\Debug\day6.pdb
文件 180 2010-03-21 09:40 实习\c#\Projects\1day6简便试修改\1day6\obj\Debug\day6.Properties.Resources.resources
文件 463 2010-03-21 15:38 实习\c#\Projects\1day6简便试修改\1day6\Program.cs
文件 1180 2010-03-21 09:20 实习\c#\Projects\1day6简便试修改\1day6\Properties\AssemblyInfo.cs
文件 2862 2010-03-21 09:20 实习\c#\Projects\1day6简便试修改\1day6\Properties\Resources.Designer.cs
文件 5612 2010-03-21 09:20 实习\c#\Projects\1day6简便试修改\1day6\Properties\Resources.resx
文件 1087 2010-03-21 09:20 实习\c#\Projects\1day6简便试修改\1day6\Properties\Settings.Designer.cs
文件 249 2010-03-21 09:20 实习\c#\Projects\1day6简便试修改\1day6\Properties\Settings.settings
文件 901 2010-03-21 09:20 实习\c#\Projects\1day6简便试修改\day6.sln
..A..H. 13312 2010-03-21 14:20 实习\c#\Projects\1day6简便试修改\day6.suo
文件 32768 2010-03-21 20:54 实习\c#\Projects\2day6简便试修改\2day6\day6\bin\Debug\day6.exe
文件 52736 2010-03-21 20:54 实习\c#\Projects\2day6简便试修改\2day6\day6\bin\Debug\day6.pdb
文件 5632 2005-12-08 14:51 实习\c#\Projects\2day6简便试修改\2day6\day6\bin\Debug\day6.vshost.exe
文件 3554 2010-03-21 15:28 实习\c#\Projects\2day6简便试修改\2day6\day6\day6.csproj
文件 5319 2010-03-21 20:54 实习\c#\Projects\2day6简便试修改\2day6\day6\Form1.cs
............此处省略1806个文件信息
评论
共有 条评论