C#题目编程,急急急!!!
1,使用SqlConnection,SqlCommand,SqlDataReader和SqlParameter,写一个方法从数据库中读数据。2,定义一个泛型集合类。要求:...
1, 使用SqlConnection,SqlCommand,SqlDataReader和SqlParameter,写一个方法从数据库中读数据。
2, 定义一个泛型集合类。要求:实现Ienumerable<T>接口,T是值类型,并T取2个类型分别测试。
3,在Graphics绘图面中心绘制一个1/4大小的红色矩形,在矩形内中心位置填充一个适当大小的蓝色椭圆,在椭圆内中心位置写上红色文字“我的绘图画”,并在矩形的4个角各绘制一个蓝色小圆。 展开
2, 定义一个泛型集合类。要求:实现Ienumerable<T>接口,T是值类型,并T取2个类型分别测试。
3,在Graphics绘图面中心绘制一个1/4大小的红色矩形,在矩形内中心位置填充一个适当大小的蓝色椭圆,在椭圆内中心位置写上红色文字“我的绘图画”,并在矩形的4个角各绘制一个蓝色小圆。 展开
展开全部
1.
public static SqlDataReader MyReader(string strSQL)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(strSQL, connection);
try
{
connection.Open();
SqlDataReader myReader = cmd.ExecuteReader();
return myReader;
}
catch (System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
这是一个reader的方法。
如果我想从数据库中读出网站配置信息,然后显示在aspx文件的文本框内。该怎么写?
我现在是这么写的:
string sqlcmd = "select * from system_config";
SqlDataReader reader = myData.MyReader(sqlcmd);
if (reader.Read())
{
sitename.Value = reader["sitename"].ToString();
en_sitename.Value = reader["en_sitename"].ToString();
company_name.Value = reader["company_name"].ToString();
en_company_name.Value = reader["en_company_name"].ToString();
company_tel.Value = reader["company_tel"].ToString();
company_fax.Value = reader["company_fax"].ToString();
}
运行后在if (reader.Read()) 这行代码提示阅读器已经关闭。我知道那个reader方法在执行完sql语句后,就关闭连接了。我下面再取的时候就出错了。我在想我这种模式应该怎么实现?
如果我把方法中finally
{
cmd.Dispose();
connection.Close();
}
去掉的话,就需要自己在程序中手工关闭连接。但是我在想是否有更好的实现模式?想了很久了没有解决。
2.给你个只实现IEnumerable <T>的例子...
C# code
class sample : IEnumerable <int>
{
private List <int> i;
public sample()
{
i = new List <int>();
i.AddRange(new int[] { 1, 2, 3, 4, 5 });
}
public IEnumerator <int> GetEnumerator()
{
return i.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return i.GetEnumerator();
}
}
3.
using System;
using System.Drawing;
using System.Drawing.Text;
class haidao{
static void Main(){
Bitmap bmp=new Bitmap(800,800);
Graphics g=Graphics.FromImage(bmp);
FontFamily fontfml=new FontFamily(GenericFontFamilies.Serif);
Font font=new Font(fontfml,20);
SolidBrush brush=new SolidBrush(Color.Red);
g.DrawString("我的绘画图",font,brush,50,10);
Random rnd=new Random();
Point[] haidao=new Point[4];
haidao[0]=new Point(rnd.Next(600)+100,rnd.Next(600)+100);
haidao[1]=new Point(rnd.Next(600)+100,rnd.Next(600)+100);
haidao[2]=new Point(rnd.Next(600)+100,rnd.Next(600)+100);
haidao[3]=new Point(rnd.Next(600)+100,rnd.Next(600)+100);
Pen curvepen=new Pen(Color.Red);
curvepen.Width=3;
g.DrawRectangle(curvepen,haidao); //空心矩形
SolidBrush myBrush=new SolidBrush(Color.Blue);
g.FillEllipse(myBrush, new Rectangle(0,0,100,200));//画实心椭圆
bmp.Save("haidao.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
public static SqlDataReader MyReader(string strSQL)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(strSQL, connection);
try
{
connection.Open();
SqlDataReader myReader = cmd.ExecuteReader();
return myReader;
}
catch (System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
这是一个reader的方法。
如果我想从数据库中读出网站配置信息,然后显示在aspx文件的文本框内。该怎么写?
我现在是这么写的:
string sqlcmd = "select * from system_config";
SqlDataReader reader = myData.MyReader(sqlcmd);
if (reader.Read())
{
sitename.Value = reader["sitename"].ToString();
en_sitename.Value = reader["en_sitename"].ToString();
company_name.Value = reader["company_name"].ToString();
en_company_name.Value = reader["en_company_name"].ToString();
company_tel.Value = reader["company_tel"].ToString();
company_fax.Value = reader["company_fax"].ToString();
}
运行后在if (reader.Read()) 这行代码提示阅读器已经关闭。我知道那个reader方法在执行完sql语句后,就关闭连接了。我下面再取的时候就出错了。我在想我这种模式应该怎么实现?
如果我把方法中finally
{
cmd.Dispose();
connection.Close();
}
去掉的话,就需要自己在程序中手工关闭连接。但是我在想是否有更好的实现模式?想了很久了没有解决。
2.给你个只实现IEnumerable <T>的例子...
C# code
class sample : IEnumerable <int>
{
private List <int> i;
public sample()
{
i = new List <int>();
i.AddRange(new int[] { 1, 2, 3, 4, 5 });
}
public IEnumerator <int> GetEnumerator()
{
return i.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return i.GetEnumerator();
}
}
3.
using System;
using System.Drawing;
using System.Drawing.Text;
class haidao{
static void Main(){
Bitmap bmp=new Bitmap(800,800);
Graphics g=Graphics.FromImage(bmp);
FontFamily fontfml=new FontFamily(GenericFontFamilies.Serif);
Font font=new Font(fontfml,20);
SolidBrush brush=new SolidBrush(Color.Red);
g.DrawString("我的绘画图",font,brush,50,10);
Random rnd=new Random();
Point[] haidao=new Point[4];
haidao[0]=new Point(rnd.Next(600)+100,rnd.Next(600)+100);
haidao[1]=new Point(rnd.Next(600)+100,rnd.Next(600)+100);
haidao[2]=new Point(rnd.Next(600)+100,rnd.Next(600)+100);
haidao[3]=new Point(rnd.Next(600)+100,rnd.Next(600)+100);
Pen curvepen=new Pen(Color.Red);
curvepen.Width=3;
g.DrawRectangle(curvepen,haidao); //空心矩形
SolidBrush myBrush=new SolidBrush(Color.Blue);
g.FillEllipse(myBrush, new Rectangle(0,0,100,200));//画实心椭圆
bmp.Save("haidao.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
展开全部
3
Graphics g = e.Graphics;
Rectangle rect = e.ClipRectangle;
int wd = rect.Width / 2;
int x = wd / 2;
int ht = rect.Height / 2;
int y = ht / 2;
rect = new Rectangle(x, y, wd, ht);
Rectangle innerRect;
int wd2 = rect.Width *3/ 4;
int ht2 = rect.Height *3/ 4;
int y2 = (rect.Top + rect.Bottom) / 2 - ht2 / 2;
int x2 = (rect.Left + rect.Right) / 2 - wd2 / 2;
innerRect = new Rectangle(x2, y2, wd2, ht2);
Pen myPen = new Pen(Color.Red, 1);
g.DrawRectangle(myPen, rect);
Brush myBrush = new SolidBrush(Color.Blue);
g.FillEllipse(myBrush, innerRect);
int radius = (rect.Width < rect.Height ? rect.Width : rect.Height) / 8;
Pen bluePen = new Pen(Color.Blue, 1);
g.DrawEllipse(bluePen, rect.Left - radius, rect.Top - radius,2 * radius, 2 * radius);
g.DrawEllipse(bluePen, rect.Right - radius, rect.Top - radius, 2 * radius, 2 * radius);
g.DrawEllipse(bluePen, rect.Right -radius,rect.Bottom - radius, 2 * radius, 2 * radius);
g.DrawEllipse(bluePen, rect.Left - radius,rect.Bottom - radius, 2 * radius, 2 * radius);
Font font=new Font("宋体",18,GraphicsUnit.Pixel);
Brush wordBrush =new SolidBrush(Color.Red);
;
StringFormat stringFormat=new StringFormat() ;
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
g.DrawString("我的绘图画", font, wordBrush, innerRect, stringFormat);
2.
class GenericAssmble<T> : IEnumerable<T>
{
private T[] container;
private int mCount;
private int mSize;
public GenericAssmble():this(10)
{
}
public GenericAssmble(int size)
{
container = new T[size];
mCount = 0;
this.mSize = size;
}
public int Lenght
{
get
{
return mCount;
}
}
public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < mCount; i++)
yield return container[i];
}
IEnumerator IEnumerable.GetEnumerator()
{
foreach (T temp in container)
yield return temp;
}
public T this[int index]
{
get
{
if (index >= 0 && index < mCount)
return container[index];
else
throw new IndexOutOfRangeException("下标越界");
}
set{
if(index>=0&&index<mCount)
container[index]=value;
else
throw new IndexOutOfRangeException("下标越界");
}
}
public bool Add(T temp)
{
if (mCount < mSize)
{
container[mCount++] = temp;
return true;
}
else
{
return false;
}
}
public bool Delete()
{
if (mCount <= 0)
return false;
mCount--;
return true;
}
public bool InsertIn(int index, T temp)
{
if (index < 0 || index > mCount||mCount>=mSize)
return false;
for (int i = mCount - 1; i >= index; i--)
container[i + 1] = container[i];
container[index] = temp;
mCount++;
return true;
}
public bool RemoveAt(int index)
{
if (index < 0 || index >= mCount)
return false;
for (int i = index; i <mCount - 1; i++)
container[i] = container[i + 1];
mCount--;
return true;
}
public int Size
{
get
{
return mSize;
}
}
1
string connString = @"server =.;
integrated security =true;
database =northwind";
string qry = @"select * from employees
where lastName=@lastname";
SqlConnection conn= new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(qry, conn);
SqlParameter lastname = new SqlParameter("@lastname", "tan");
cmd.Parameters.Add(lastname);
conn.Open();
SqlDataReader read = cmd.ExecuteReader();
Graphics g = e.Graphics;
Rectangle rect = e.ClipRectangle;
int wd = rect.Width / 2;
int x = wd / 2;
int ht = rect.Height / 2;
int y = ht / 2;
rect = new Rectangle(x, y, wd, ht);
Rectangle innerRect;
int wd2 = rect.Width *3/ 4;
int ht2 = rect.Height *3/ 4;
int y2 = (rect.Top + rect.Bottom) / 2 - ht2 / 2;
int x2 = (rect.Left + rect.Right) / 2 - wd2 / 2;
innerRect = new Rectangle(x2, y2, wd2, ht2);
Pen myPen = new Pen(Color.Red, 1);
g.DrawRectangle(myPen, rect);
Brush myBrush = new SolidBrush(Color.Blue);
g.FillEllipse(myBrush, innerRect);
int radius = (rect.Width < rect.Height ? rect.Width : rect.Height) / 8;
Pen bluePen = new Pen(Color.Blue, 1);
g.DrawEllipse(bluePen, rect.Left - radius, rect.Top - radius,2 * radius, 2 * radius);
g.DrawEllipse(bluePen, rect.Right - radius, rect.Top - radius, 2 * radius, 2 * radius);
g.DrawEllipse(bluePen, rect.Right -radius,rect.Bottom - radius, 2 * radius, 2 * radius);
g.DrawEllipse(bluePen, rect.Left - radius,rect.Bottom - radius, 2 * radius, 2 * radius);
Font font=new Font("宋体",18,GraphicsUnit.Pixel);
Brush wordBrush =new SolidBrush(Color.Red);
;
StringFormat stringFormat=new StringFormat() ;
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
g.DrawString("我的绘图画", font, wordBrush, innerRect, stringFormat);
2.
class GenericAssmble<T> : IEnumerable<T>
{
private T[] container;
private int mCount;
private int mSize;
public GenericAssmble():this(10)
{
}
public GenericAssmble(int size)
{
container = new T[size];
mCount = 0;
this.mSize = size;
}
public int Lenght
{
get
{
return mCount;
}
}
public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < mCount; i++)
yield return container[i];
}
IEnumerator IEnumerable.GetEnumerator()
{
foreach (T temp in container)
yield return temp;
}
public T this[int index]
{
get
{
if (index >= 0 && index < mCount)
return container[index];
else
throw new IndexOutOfRangeException("下标越界");
}
set{
if(index>=0&&index<mCount)
container[index]=value;
else
throw new IndexOutOfRangeException("下标越界");
}
}
public bool Add(T temp)
{
if (mCount < mSize)
{
container[mCount++] = temp;
return true;
}
else
{
return false;
}
}
public bool Delete()
{
if (mCount <= 0)
return false;
mCount--;
return true;
}
public bool InsertIn(int index, T temp)
{
if (index < 0 || index > mCount||mCount>=mSize)
return false;
for (int i = mCount - 1; i >= index; i--)
container[i + 1] = container[i];
container[index] = temp;
mCount++;
return true;
}
public bool RemoveAt(int index)
{
if (index < 0 || index >= mCount)
return false;
for (int i = index; i <mCount - 1; i++)
container[i] = container[i + 1];
mCount--;
return true;
}
public int Size
{
get
{
return mSize;
}
}
1
string connString = @"server =.;
integrated security =true;
database =northwind";
string qry = @"select * from employees
where lastName=@lastname";
SqlConnection conn= new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(qry, conn);
SqlParameter lastname = new SqlParameter("@lastname", "tan");
cmd.Parameters.Add(lastname);
conn.Open();
SqlDataReader read = cmd.ExecuteReader();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace HuiPublicing.SQLServerDAL //可以修改成实际项目的命名空间名称
{
/// <summary>
/// Copyright (C) 2004-2008 LiTianPing
/// 数据访问基础类(基于SQLServer)
/// 用户可以修改满足自己项目的需要。
/// </summary>
public abstract class DbHelperSQL
{
//数据库连接字符串(web.config来配置)
//<add key="ConnectionString" value="server=127.0.0.1;database=DATABASE;uid=sa;pwd=" />
protected static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
public DbHelperSQL()
{
}
#region 执行简单SQL语句
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString,connection))
{
try
{
connection.Open();
int rows=cmd.ExecuteNonQuery();
return rows;
}
catch(System.Data.SqlClient.SqlException E)
{
connection.Close();
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>
public static void ExecuteSqlTran(ArrayList SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection=conn;
SqlTransaction tx=conn.BeginTransaction();
cmd.Transaction=tx;
try
{
for(int n=0;n<SQLStringList.Count;n++)
{
string strsql=SQLStringList[n].ToString();
if (strsql.Trim().Length>1)
{
cmd.CommandText=strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch(System.Data.SqlClient.SqlException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
conn.Close();
}
}
}
---写不下了
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace HuiPublicing.SQLServerDAL //可以修改成实际项目的命名空间名称
{
/// <summary>
/// Copyright (C) 2004-2008 LiTianPing
/// 数据访问基础类(基于SQLServer)
/// 用户可以修改满足自己项目的需要。
/// </summary>
public abstract class DbHelperSQL
{
//数据库连接字符串(web.config来配置)
//<add key="ConnectionString" value="server=127.0.0.1;database=DATABASE;uid=sa;pwd=" />
protected static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
public DbHelperSQL()
{
}
#region 执行简单SQL语句
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString,connection))
{
try
{
connection.Open();
int rows=cmd.ExecuteNonQuery();
return rows;
}
catch(System.Data.SqlClient.SqlException E)
{
connection.Close();
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>
public static void ExecuteSqlTran(ArrayList SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection=conn;
SqlTransaction tx=conn.BeginTransaction();
cmd.Transaction=tx;
try
{
for(int n=0;n<SQLStringList.Count;n++)
{
string strsql=SQLStringList[n].ToString();
if (strsql.Trim().Length>1)
{
cmd.CommandText=strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch(System.Data.SqlClient.SqlException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
conn.Close();
}
}
}
---写不下了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询