2

I'm writing an application that reads from a MySQL database and migrates data into a SQL Server database. I've chosen to write this in C# because of it's built in SQL structures and functionality. I was wondering, would it be possible to use the System.Data.SqlTypes.* structures in my classes where data from MySql will be read into?

ie.

// Pseudo code
IDataReader reader = /* { return DB reader } */;

while (reader.Read())
{
  SqlString str = (SqlString) reader["someVarCharField"];
  SqlBoolean b = (SqlBoolean) reader["someTinyIntField"];
}

Will this work or is there a better way of doing this?

4
  • I did something very similar a few years ago, and from what I recall I just used the built-in types. No need for the Sql specific types. Commented Jan 9, 2012 at 20:41
  • The only problem is converting TinyINT to Boolean (built-in) because there is no conversion from a string of "1" to true. I'm wondering if there is a simpler way to do this. Commented Jan 9, 2012 at 20:42
  • I did use the .net types, like string, long, boolean etc. In my case, for boolean i used Ctype in vb .net. => Ctype("1", boolean) Commented Jan 9, 2012 at 20:45
  • @MarkP You can use the Convert.ToBoolean() method. dotnetperls.com/convert-int-bool Commented Jan 9, 2012 at 20:59

2 Answers 2

2

If you know the MySqlDbType it is arguably best practice to simply do an Explicit conversion to a .Net type then use the correct SqlDbType on inserting the data into SqlServer. This removes any issues with casting, null references etc.

string myStringField = reader["someVarCharField"].ToString();
bool myBoolField = reader.GetBoolean("booleanField");
etc....
Sign up to request clarification or add additional context in comments.

Comments

1

I was thinking what if you just did something like, psudeo code

inside Sql Class...

while(reader.Read())
{
   int id = int.Parse(reader[0].ToString());
   string name = reader[1];
   bool active = false;
   if(int.Parse(reader[2].ToString()) == 1)
      active = true;

   SqlServerDBInsert(id, name, active);
}

... The insert

SQLServerDBInsert(int id, string name, bool active)
{

   string query = "INSERT INTO MyTable(ID, Name, Active) VALUES(@id, @name, @active)";
   using(SqlCommand cmd = new SqlCommand(query, conn))
   {
     SqlParamter param = new SqlParameter();
     param.ParameterName = "@id";
     param.Value = id;
     param.SqlDbType = SqlDbType.Int;
     cmd.Parameters.Add(param);
     ....
   }
}

I'm not sure about having a connection open to MySQL and SQLServer at the same time so there is that to be worried about.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.