想实现一个游戏的登录和注册功能,经过测试,已经成功连接上了sql sever了,代码如下:
void Start()
{
//连接SQL
string s = @String.Format("server = {0};database = {1};uid = {2};pwd = {3};", host, databaseName, ID, pwd); ;
connection = new SqlConnection(s);
connection.Open();
}
实现注册功能的思路是,先查看数据库中是否有同名的账号,否,则调用插入函数插入账号密码到数据库中
//查询结果以DataSet类返回
public DataSet sqlCheck(string something,string tablename)
{
string sentense = " select "+something+" from "+tablename;
SqlDataAdapter _adapter = new SqlDataAdapter(sentense, connection);
DataSet ds = new DataSet();
_adapter.Fill(ds, tablename);
return ds;
}
public DataSet sqlInsert(string values,string tablename)
{
string sentense = " insert into " + tablename + " values (" + values + ")";
SqlDataAdapter _adapter = new SqlDataAdapter(sentense, connection);
DataSet ds = new DataSet();
_adapter.Fill(ds, tablename);
return ds;
}
如果分别测试两个方法,发现可以正常查看或者插入数据,但是一旦先调用sqlCheck()再调用sqlInsert()就会报错:
NullReferenceException: Object reference not set to an instance of an object
System.Data.Common.DataAdapter.BuildSchema (IDataReader reader, System.Data.DataTable table, SchemaType schemaType, MissingSchemaAction missingSchAction, MissingMappingAction missingMapAction, System.Data.Common.DataTableMappingCollection dtMapping)
System.Data.Common.DataAdapter.BuildSchema (IDataReader reader, System.Data.DataTable table, SchemaType schemaType)
System.Data.Common.DataAdapter.FillTable (System.Data.DataTable dataTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords, System.Int32& counter)
System.Data.Common.DataAdapter.FillInternal (System.Data.DataSet dataSet, System.String srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
System.Data.Common.DataAdapter.Fill (System.Data.DataSet dataSet, System.String srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
System.Data.Common.DbDataAdapter.Fill (System.Data.DataSet dataSet, Int32 startRecord, Int32 maxRecords, System.String srcTable, IDbCommand command, CommandBehavior behavior)
System.Data.Common.DbDataAdapter.Fill (System.Data.DataSet dataSet, System.String srcTable)
(wrapper remoting-invoke-with-check) System.Data.Common.DbDataAdapter:Fill (System.Data.DataSet,string)
困扰了很多天了,求助求助!